What is the value of vehicle?get a NullPointerException
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.
What is the value of vehicle?get a NullPointerException
If you don't understand my answer, don't ignore it, ask a question.
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.
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.
Ok so in pAddVehicle class
private Vehicle vehicle;
Is instantiating the Vehicle class to use, right?
but in my Vehicle class, this
is my constructor.public Vehicle() { count = 0; }
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.
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.Is instantiating the Vehicle class to use, right?
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.
I'll come back later. I am taking a break For food and to clear my head, like has been written.
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.
Post the code that you are asking about.
If you don't understand my answer, don't ignore it, ask a question.
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; } }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.
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.
I keep getting a NullPointerException at this line, which is in the pAddVehicle class
vehicle.addItem(ma, mo,co, numD, numC, p);
You have been through this type of problem before.I keep getting a NullPointerException at this line
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.
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
is just assigning a variable 'vehicle' to refer to the class Vehicle.private Vehicle vehicle;
It just hit me. I commented out the line I just put in the code above and added this line
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?Vehicle vehicle = new Vehicle();
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.
What does that mean? You don't call a variable, you call a method in a class or a constructor for a class.when the vehicle is called,
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.