Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 2 of 2 FirstFirst 12
Results 26 to 45 of 45

Thread: Having a NullPointerException

  1. #26
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Having a NullPointerException

    get a NullPointerException
    What is the value of vehicle?
    If you don't understand my answer, don't ignore it, ask a question.

  2. #27
    Member
    Join Date
    May 2014
    Posts
    31
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Having a NullPointerException

    Quote Originally Posted by Norm View Post
    What is the value of vehicle?
    It is also null. I've referenced it and such, just like you were saying with the other problem. It is listed.

    Sorry, just getting frustrated with this one. I've even tried stepping on to another one and having issues there now.

  3. #28
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Having a NullPointerException

    Go back through the code and find out why the null variable never got a valid value.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #29
    Member
    Join Date
    May 2014
    Posts
    31
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Having a NullPointerException

    Quote Originally Posted by Norm View Post
    Go back through the code and find out why the null variable never got a valid value.
    I'm sorry, I don't see why it didn't.

  5. #30
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Having a NullPointerException

    If it has a null value, then it was not assigned a valid value.
    Is it ever assigned a value?
    Is it used BEFORE it is assigned a value?
    If you don't understand my answer, don't ignore it, ask a question.

  6. #31
    Member
    Join Date
    May 2014
    Posts
    31
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Having a NullPointerException

    Quote Originally Posted by Norm View Post
    If it has a null value, then it was not assigned a valid value.
    Is it ever assigned a value?
    Is it used BEFORE it is assigned a value?
    Ok so in pAddVehicle class

    private Vehicle vehicle;

    Is instantiating the Vehicle class to use, right?

    but in my Vehicle class, this
    public Vehicle()
        {
            count = 0;
        }
    is my constructor.

    So when vehicle is called, the constructor just sets up for count and not the other variables?

    Is my thinking on the right track?

    I am trying to have the user enter these and it sends them to a Vector. My second tabbed panel is supposed to constantly display what is in the Vector.

  7. #32
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Having a NullPointerException

    Is instantiating the Vehicle class to use, right?
    No, that defines a variable that CAN refer to a Vehicle object. No object is created there. The new statement is used to create instances of a class.

    This statement in the main() method creates an instance:
     vehicle = new Vehicle();
    If you don't understand my answer, don't ignore it, ask a question.

  8. #33
    Member
    Join Date
    May 2014
    Posts
    31
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Having a NullPointerException

    I'll come back later. I am taking a break For food and to clear my head, like has been written.

  9. #34
    Member
    Join Date
    May 2014
    Posts
    31
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Having a NullPointerException

    Quote Originally Posted by Norm View Post
    No, that defines a variable that CAN refer to a Vehicle object. No object is created there. The new statement is used to create instances of a class.

    This statement in the main() method creates an instance:
     vehicle = new Vehicle();
    Ok, so I am back. The break worked somewhat, I got my other project done fast. This one is still stumping me.

    So with that statement in main creating an instance of the Vehicle class and my constructor having only a count in it, is that the root of my problem?

  10. #35
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Having a NullPointerException

    Using a variable with a null value is the cause of the NullPointerException.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #36
    Member
    Join Date
    May 2014
    Posts
    31
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Having a NullPointerException

    Quote Originally Posted by Norm View Post
    Using a variable with a null value is the cause of the NullPointerException.
    Then I am confused. I thought if there was not a value assigned, it would refer to the constructor?

  12. #37
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Having a NullPointerException

    Post the code that you are asking about.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #38
    Member
    Join Date
    May 2014
    Posts
    31
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Having a NullPointerException

    Quote Originally Posted by Norm View Post
    Post the code that you are asking about.
    import javax.swing.*;
     
    public class PRJ03 extends JFrame
    {
        public static void main (String [] args)
        {
            Vehicle vehicle;
            pAddVehicle pnlAdd;
            pShowVehicle pnlShow;
     
            JTabbedPane tpDisplay;
     
            vehicle = new Vehicle();
            pnlAdd = new pAddVehicle();
            pnlShow = new pShowVehicle();
            tpDisplay = new JTabbedPane(JTabbedPane.BOTTOM);
     
            pnlAdd.setInventory(vehicle);
     
            tpDisplay.addTab("Add Vehicle", pnlAdd);
            tpDisplay.addTab("Vehicles", pnlShow);
     
            PRJ03 frmApp = new PRJ03();
            frmApp.add(tpDisplay);
     
            frmApp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frmApp.setSize(700,500);
            frmApp.setVisible(true);
     
        }
    }
    import java.util.Vector;
     
    public class Vehicle
    {
        private int count;
        SingleVehicle tempCar;
     
        Vector <SingleVehicle> vVehicle = new Vector<SingleVehicle>();
     
        public Vehicle()
        {
            count = 0;
        }
     
        public int getCount()
        {
            return count;
        }
     
        public void addItem(String make, String model, String color, int numDoors, int numCylinders, double price)throws VehicleException
        {
            tempCar = new SingleVehicle(make, model, color, numDoors, numCylinders, price);
            vVehicle.add(tempCar);
            count++;
     
        }
    }
    public class SingleVehicle
    {
        String make = "";
        String model = "";
        String color = "";
        int numDoors = 0;
        int numCylinders = 0;
        double price = 0.0;
     
        public SingleVehicle()
        {
            this.make = "";
            this.model = "";
            this.color = "";
            this.numDoors = 0;
            this.numCylinders = 0;
            this.price = 0.0;
        }
     
        public SingleVehicle(String make, String model, String color, int numDoors, int numCylinders, double price)
        {
            this.make = make;
            this.model = model;
            this.color = color;
            this.numDoors = numDoors;
            this.numCylinders = numCylinders;
            this.price = price;
        }
     
        public double getPrice() {
            return price;
        }
     
        public void setPrice(double price) {
            this.price = price;
        }
     
        public String getMake() {
            return make;
        }
     
        public void setMake(String make) {
            this.make = make;
        }
     
        public String getModel() {
            return model;
        }
     
        public void setModel(String model) {
            this.model = model;
        }
     
        public String getColor() {
            return color;
        }
     
        public void setColor(String color) {
            this.color = color;
        }
     
        public int getNumDoors() {
            return numDoors;
        }
     
        public void setNumDoors(int numDoors) {
            this.numDoors = numDoors;
        }
     
        public int getNumCylinders() {
            return numCylinders;
        }
     
        public void setNumCylinders(int numCylinders) {
            this.numCylinders = numCylinders;
        }
    }
    public class VehicleException extends Exception
    {
        private String sError;
     
        public VehicleException()
        {
            sError = "Unknown Inventory Error Occurred";
        }
        public VehicleException(String sError)
        {
            super(sError);
            this.sError = sError;
        }
     
        public String toString()
        {
            return sError;
        }
    }
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    public class pAddVehicle extends JPanel implements ActionListener
    {
        private Vehicle vehicle;
        private pShowVehicle pnlShow;
     
        private JLabel lblMake;
        private JLabel lblModel;
        private JLabel lblColor;
        private JLabel lblNumDoors;
        private JLabel lblNumCylinders;
        private JLabel lblPrice;
     
        private JTextField txtMake;
        private JTextField txtModel;
        private JTextField txtColor;
        private JTextField txtNumDoors;
        private JTextField txtNumCylinders;
        private JTextField txtPrice;
        private JTextField txtDisplay;
     
        private JButton btnAddItem;
     
        private JPanel ROW1;
        private JPanel ROW2;
        private JPanel ROW3;
        private JPanel ROW4;
        private JPanel ROW5;
        private JPanel ROW6;
        private JPanel ROW7;
        private JPanel ROW8;
     
        public pAddVehicle()
        {
            lblMake = new JLabel("Car's Make");
            lblModel = new JLabel("Car's Model");
            lblColor = new JLabel("Car's Color");
            lblNumDoors = new JLabel("Number of Doors");
            lblNumCylinders = new JLabel("Number of Cylinders");
            lblPrice = new JLabel("Price");
     
            txtMake = new JTextField(20);
            txtModel = new JTextField(20);
            txtColor = new JTextField(10);
            txtNumDoors = new JTextField(2);
            txtNumCylinders = new JTextField(2);
            txtPrice = new JTextField(5);
            txtDisplay = new JTextField(50);
     
            btnAddItem = new JButton("Add to Inventory");
     
            ROW1 = new JPanel();
            ROW2 = new JPanel();
            ROW3 = new JPanel();
            ROW4 = new JPanel();
            ROW5 = new JPanel();
            ROW6 = new JPanel();
            ROW7 = new JPanel();
            ROW8 = new JPanel();
     
            btnAddItem.setActionCommand("additem");
            btnAddItem.addActionListener(this);
     
            ROW1.add(lblMake);
            ROW1.add(txtMake);
            ROW2.add(lblModel);
            ROW2.add(txtModel);
            ROW3.add(lblColor);
            ROW3.add(txtColor);
            ROW4.add(lblNumDoors);
            ROW4.add(txtNumDoors);
            ROW5.add(lblNumCylinders);
            ROW5.add(txtNumCylinders);
            ROW6.add(lblPrice);
            ROW6.add(txtPrice);
            ROW7.add(btnAddItem);
            ROW8.add(txtDisplay);
     
            setLayout(new GridLayout(8, 1));
            add(ROW1);
            add(ROW2);
            add(ROW3);
            add(ROW4);
            add(ROW5);
            add(ROW6);
            add(ROW7);
            add(ROW8);
        }
     
        public void setInventory(Vehicle i)
        {
            vehicle = i;
        }
     
        public void setOrderPanel(pShowVehicle p)
        {
            pnlShow = p;
        }
     
        @Override
        public void actionPerformed(ActionEvent evt)
        {
            String ma;
            String mo;
            String co;
            int numD;
            int numC;
            double p;
     
            if (evt.getActionCommand().equals("additem"))
            {
                try
                {
                    txtDisplay.setText("");
                    ma = txtMake.getText();
                    mo = txtModel.getText();
                    co = txtColor.getText();
                    numD = Integer.parseInt(txtNumDoors.getText());
                    numC = Integer.parseInt(txtNumCylinders.getText());
                    p = Double.parseDouble(txtPrice.getText());
     
                    vehicle.addItem(ma, mo,co, numD, numC, p);
                    txtDisplay.setText("Vehicle added");
                    pnlShow.doSomething();
                }
                catch (VehicleException ex)
                {
                    txtDisplay.setText(ex.toString());
                }
                catch (NumberFormatException ex)
                {
                    txtDisplay.setText("Please Type Valid Numbers!");
                }
                catch (Exception ex)
                {
                    txtDisplay.setText(ex.toString());
                }
     
            }
        }
    }
    import javax.swing.*;
     
    public class pShowVehicle extends JPanel
    {
        private Vehicle vehicle;
     
        JScrollPane spOrder;
        JTextArea taOrder;
     
        JPanel ROW1;
     
        public pShowVehicle()
        {
            taOrder = new JTextArea(20, 60);
            spOrder = new JScrollPane(taOrder);
            ROW1 = new JPanel();
            ROW1.add(spOrder);
            add(ROW1);
        }
     
        public void doSomething()
        {
            taOrder.setText("ZZZZZZZZZZZ");
        }
    }

    Yes, me again, and of course I am still confused and lost. If I can't understand this soon, I am done for in the class. I used our in class project, since it was similar, to set this up. All my calls are similar to that one. I just don't understand what I did wrong and how.

  14. #39
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Having a NullPointerException

    What line(s) of code in all that was posted are you talking about?
    What problem are you working on?
    If you don't understand my answer, don't ignore it, ask a question.

  15. #40
    Member
    Join Date
    May 2014
    Posts
    31
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Having a NullPointerException

    I keep getting a NullPointerException at this line, which is in the pAddVehicle class
     vehicle.addItem(ma, mo,co, numD, numC, p);

  16. #41
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Having a NullPointerException

    I keep getting a NullPointerException at this line
    You have been through this type of problem before.
    Is the value of vehicle null?
    Why is it null?
    Where should the code have assigned it a value?
    It needs a value BEFORE it is used.

    I don't see the calls to the printStackTrace() method in the catch blocks in post#38. What happened to them?
    If you don't understand my answer, don't ignore it, ask a question.

  17. #42
    Member
    Join Date
    May 2014
    Posts
    31
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Having a NullPointerException

    Quote Originally Posted by Norm View Post
    You have been through this type of problem before.
    Is the value of vehicle null?
    Why is it null?
    Where should the code have assigned it a value?

    I don't see the calls to the printStackTrace() method in the catch blocks in post#38. What happened to them?
    I cut and pasted from my first post, sorry. Adding those methods were the only things I did on my copy.

    This is the same problem I've been having since yesterday. I am sorry; I am just not understanding what you are saying. I understand that this
    private Vehicle vehicle;
    is just assigning a variable 'vehicle' to refer to the class Vehicle.

    It just hit me. I commented out the line I just put in the code above and added this line
    Vehicle vehicle = new Vehicle();
    below it. I ran it and got no error, it popped up as need be. So is that what you were hinting at me to do?

  18. #43
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Having a NullPointerException

    The variable needs a non-null value when it is used.

    I have no idea if that line of code will work in your program. The line is completely out of context of the rest of the code.
    If you don't understand my answer, don't ignore it, ask a question.

  19. #44
    Member
    Join Date
    May 2014
    Posts
    31
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Having a NullPointerException

    Quote Originally Posted by Norm View Post
    The variable needs a non-null value when it is used.

    I have no idea if that line of code will work in your program. The line is completely out of context of the rest of the code.
    Then I don't get it.

    'vehicle' is the variable of the class Vehicle, right?
    Obviously I am wrong, but when the vehicle is called, wouldn't it pull the constructor of the class Vehicle?

  20. #45
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Having a NullPointerException

    when the vehicle is called,
    What does that mean? You don't call a variable, you call a method in a class or a constructor for a class.

    In your code: vehicle is a variable of type Vehicle. It can hold a reference to an instance of the Vehicle class. An instance of a class is created by the new statement. The new statement returns a reference to the object that was created that can be assigned to a variable.
    If you don't understand my answer, don't ignore it, ask a question.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. why am I getting NullPointerException.
    By mia_tech in forum What's Wrong With My Code?
    Replies: 9
    Last Post: October 24th, 2012, 11:33 AM
  2. NullPointerException
    By Alket in forum Member Introductions
    Replies: 1
    Last Post: June 7th, 2012, 07:09 AM
  3. NullPointerException
    By deathmatex in forum Exceptions
    Replies: 4
    Last Post: March 27th, 2012, 03:54 AM
  4. [SOLVED] NullPointerException
    By javapenguin in forum What's Wrong With My Code?
    Replies: 13
    Last Post: October 1st, 2010, 12:10 AM
  5. NullPointerException
    By bbr201 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 29th, 2010, 07:06 PM