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 1 of 2 12 LastLast
Results 1 to 25 of 45

Thread: Having a NullPointerException

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

    Default Having a NullPointerException

    Ok, I am trying to do another class project and I keep getting a NullPointerException showing up on the txtOutput line. The main goal of this project is to have a driver program with the 2 paned tab. One tab the user enters the data to add a Vehicle to a VECTOR and the other will have a JPanel that will display it. I know my JPanel isn't set up correctly to display. I am trying to get the first tab to work. Whenever I punch it is, I can't get Vehicle added to show up, just NullPointerException. If you notice something else, feel free to offer help as well.

    I had shoulder surgery earlier this semester and couldn't do the projects. The teacher is letting me turn them in late - by tonight. There is 6 and this is only 3. I've been up too long trying to get these done and have probably made stupid mistakes.

    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");
        }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Having a NullPointerException

    I keep getting a NullPointerException
    Please copy the full text of the error message and paste it here. It has important info about the error.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Having a NullPointerException

    Ok sorry. In my txtOutput box, I get this:

    java.lang.NullPointerException

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Having a NullPointerException

    That is not enough. You need to get the full stack trace. Be sure that the printStackTrace() method is called in all the catch blocks so that the location of the error is shown.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    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
    That is not enough. You need to get the full stack trace. Be sure that the printStackTrace() method is called in all the catch blocks so that the location of the error is shown.
    I am confused on what you are asking. My professor never explained that. It was more of a this is what you type, and this is what it does. If the program didn't do what he wanted, he got real quiet while debugging it.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Having a NullPointerException

    The print out from the printStackTrace() method will show where the error happens. If you do not know the statement where the error happens, that makes it harder to find the error.
    The other choice is to add println statements before ALL of the statements in the try{} block that print out the values of ALL the variables used. The print out will show what variable is null. Also it will help locate the statement where the exception happens because the next println() after that statement will not print anything due to the exception.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Having a NullPointerException

    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)
                {
                    printStackTrace();
                    txtDisplay.setText(ex.toString());
                }
                catch (NumberFormatException ex)
                {
                    printStackTrace();
                    txtDisplay.setText("Please Type Valid Numbers!");
                }
                catch (Exception ex)
                {
                    printStackTrace();
                    txtDisplay.setText(ex.toString());
                }
     
            }
        }
    }

    Like that? Because when I plug it in, it cannot resolve that method.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Having a NullPointerException

    it cannot resolve that method.
    The method belongs to class of object passed to the catch block.
    }catch(Exception x) {
      x.printStackTrace();
    }
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    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 method belongs to class of object passed to the catch block.
    }catch(Exception x) {
      x.printStackTrace();
    }
    I'm sorry. Like I initially said, I've been up too long looking at different projects and code, my mind is melting. Ok, when I did that, I get this

    java.lang.NullPointerException
    at pAddVehicle.actionPerformed(pAddVehicle.java:131)
    at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:2018)
    at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2341)
    at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.jav a:6505)
    at javax.swing.JComponent.processMouseEvent(JComponen t.java:3320)
    at java.awt.Component.processEvent(Component.java:627 0)
    at java.awt.Container.processEvent(Container.java:222 9)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4861)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2287)
    at java.awt.Component.dispatchEvent(Component.java:46 87)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4492)
    at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2273)
    at java.awt.Window.dispatchEventImpl(Window.java:2719 )
    at java.awt.Component.dispatchEvent(Component.java:46 87)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:735)
    at java.awt.EventQueue.access$200(EventQueue.java:103 )
    at java.awt.EventQueue$3.run(EventQueue.java:694)
    at java.awt.EventQueue$3.run(EventQueue.java:692)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:708)
    at java.awt.EventQueue$4.run(EventQueue.java:706)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 705)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:91)

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Having a NullPointerException

    java.lang.NullPointerException
    at pAddVehicle.actionPerformed(pAddVehicle.java:131)
    There is a null value when the statement on line 131 is executed. Look at line 131, find the variable with the null value and then backtrack in the code to see why that variable does not have a valid value. If you can't tell which variable has the null value, add a println before line 131 that prints the values of all the variables on line 131.
    If you don't understand my answer, don't ignore it, ask a question.

  11. The Following User Says Thank You to Norm For This Useful Post:

    meni (May 11th, 2014)

  12. #11
    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
    There is a null value when the statement on line 131 is executed. Look at line 131, find the variable with the null value and then backtrack in the code to see why that variable does not have a valid value. If you can't tell which variable has the null value, add a println before line 131 that prints the values of all the variables on line 131.
    After finding the line, it was this snippet of code:
       pnlShow.doSomething();

    Thank you so much for your help! Now to try and display the Vector

  13. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Having a NullPointerException

    What was the value in the pnlShow variable? If it was null, where should it be given a value?
    If you don't understand my answer, don't ignore it, ask a question.

  14. #13
    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 was the value in the pnlShow variable? If it was null, where should it be given a value?
    After I fixed all of it, I thought about this. It didn't click still why. Since in my pnlShow class, doSomething was written as:
    public void doSomething()
        {
            taOrder.setText("ZZZZZZZZZZZ");
        }

    Shouldn't that have shown up on my other tab panel?

  15. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Having a NullPointerException

    If that code was executed, the contents of taOrder should be "ZZZZZZZZZZZZZ"
    I haven't executed the code to know if taOrder is shown in the GUI when that code is executed.
    If you don't understand my answer, don't ignore it, ask a question.

  16. #15
    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 that code was executed, the contents of taOrder should be "ZZZZZZZZZZZZZ"
    I haven't executed the code to know if taOrder is shown in the GUI when that code is executed.
    When I ran it, it never showed up on that panel. Hmm, the method was what I was going to use to spit out the contents of the Vector.

    When I attempt to add it back, I get the NullPointerException again

  17. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Having a NullPointerException

    I get the NullPointerException
    What variable has the null value?
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Having a NullPointerException

    System.out.println(pnlShow.doSomething());

    Ok, I put this line in right before that line and it can't resolve the println void. I know I am missing something common, but I can't think what. This way of doing it is new to me, so please bear with me.

  19. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Having a NullPointerException

    That code is trying to print the value returned by the doSomething() method, but nothing is returned because it is void.
    The variable in that method is pnlShow. That is what could cause the exception and what should be printed to see if it is null.
    If you don't understand my answer, don't ignore it, ask a question.

  20. #19
    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
    That code is trying to print the value returned by the doSomething() method, but nothing is returned because it is void.
    The variable in that method is pnlShow. That is what could cause the exception and what should be printed to see if it is null.
    That variable is just declaration of the pShowVehicle panel. You can print that?

    That was new to me. I tried it and pnlShow has a null value. So i need to make a constructor on the pShowVehicle class?

  21. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Having a NullPointerException

    So i need to make a constructor on the pShowVehicle class?
    You need to assign a reference to an instance of the pShowVehicle class to the variable.
    An instance may already exist that could be used or a new one could be created with a new statement.
    If you don't understand my answer, don't ignore it, ask a question.

  22. #21
    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 need to assign a reference to an instance of the pShowVehicle class to the variable.
    An instance may already exist that could be used or a new one could be created with a new statement.
    public void setOrderPanel(pShowVehicle p)
        {
            pnlShow = p;
        }

    I thought that is what this did? The only notes I have, is the program we did in class that day. So I am looking off of it to try to piece this together.

  23. #22
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Having a NullPointerException

    It would if it were called. Where is it called?

    Why not pass the reference in the constructor?
    If you don't understand my answer, don't ignore it, ask a question.

  24. #23
    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
    It would if it were called. Where is it called?

    Why not pass the reference in the constructor?
    We never called it in our class example and it worked. That is what is throwing me off then. What constructor then?

  25. #24
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Having a NullPointerException

    What class needs the reference to a pShowVehicle object? Pass it in that class's constructor.
    If you don't understand my answer, don't ignore it, ask a question.

  26. #25
    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 class needs the reference to a pShowVehicle object? Pass it in that class's constructor.
    pnlAdd.setOrderPanel(pnlShow);

    I put that into my main program, if thinking right. And now get a NullPointerException at
     vehicle.addItem(ma, mo,co, numD, numC, p);

    which I already had done in the main program and earlier in the pAddVehicle class

Page 1 of 2 12 LastLast

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