Results 1 to 9 of 9
- 10-03-2012, 11:43 PM #1
Member
- Join Date
- Oct 2012
- Posts
- 15
- Rep Power
- 0
A little help with a NullPointerException?
Hello, everyone. I'm in a basic Java class right now learning everything, and I've run across a common runtime error in one of my programs. I just can't figure out where it's coming from. I may also have completely screwed everything up in trying to fix it. Because of the NullPointerException I haven't been able to check for logic errors or ill formatting yet. Here's the code:
Java Code:public class CarV5 { private String carType1; private int endMiles1; private int startMiles1; private double gallonsUsed1; private double pricePerGallon1; CarV5(String carType, int eMiles, int sMiles, double gallons, double pPG) { carType1 = carType; endMiles1 = eMiles; startMiles1 = sMiles; gallonsUsed1 = gallons; pricePerGallon1 = pPG; } //calculate the total distance driven public int calcDistance(int sMiles, int eMiles) { return eMiles - sMiles; } //calculate the car's gas mileage public double calcMPG(int dist, double gals) { return dist / gals; } public double calcGPM(int dist, double gals) { return gals / dist; } public double totalCost(double gPM, double cost, int dist) { return gPM * cost * dist; } //print the results of the MPG calculation public void printResults(String carName, int sMiles, int eMiles, int dist, double gals, double price, double cost, double mPG, double gPM) { System.out.printf("%78s\nGas Mileage Calculations%12sType of Car%16sStart Miles%14sEnd Miles%14sDistance%13sGallons%10Price%9Cost%14sMiles/Gal%14s\nGallons/Mi"); System.out.println("---------------------------------------------------------------------------------------------------------------------------"); System.out.printf(carName + "%19s", sMiles, "%19s", eMiles, "%18s", dist, "%17s", gals, "%12s", price, "%8s", cost, "%17s", mPG, "%16s", gPM); } public void main(String [] args) { //local variables carType1 = "2008 Volkswagen Jetta"; startMiles1 = 81730; endMiles1 = 82111; gallonsUsed1 = 10.2; pricePerGallon1 = 3.99; CarV5 car1 = new CarV5(carType1, endMiles1, startMiles1, gallonsUsed1, pricePerGallon1); int distance1 = car1.calcDistance(startMiles1, endMiles1); double milesPerGallon1 = car1.calcMPG(distance1, gallonsUsed1); double gallonsPerMile1 = car1.calcGPM(distance1, gallonsUsed1); double totalCost1 = car1.totalCost(gallonsPerMile1, pricePerGallon1, distance1); car1.printResults(carType1, startMiles1, endMiles1, distance1, gallonsUsed1, pricePerGallon1, totalCost1, milesPerGallon1, gallonsPerMile1); } }
- 10-04-2012, 02:31 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 19
Re: A little help with a NullPointerException?
The first step is to identify the line in your code where the NullPointerException was thrown.
If you can't understand the "stack trace" that the runtime provides when it tells you about the exception, post the whole thing and say which line(s) of your code it is referring to.
- 10-04-2012, 03:24 AM #3
Member
- Join Date
- Oct 2012
- Posts
- 15
- Rep Power
- 0
Re: A little help with a NullPointerException?
The error traces to lines 25 and 39, according to DrJava.
- 10-04-2012, 05:24 AM #4
Senior Member
- Join Date
- Sep 2012
- Posts
- 108
- Rep Power
- 0
Re: A little help with a NullPointerException?
post the error
- 10-04-2012, 10:31 AM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 28
- 10-04-2012, 01:41 PM #6
Member
- Join Date
- Oct 2012
- Posts
- 15
- Rep Power
- 0
Re: A little help with a NullPointerException?
I'll post the error around two this afternoon, if not earlier. I'm currently away from the computer it's saved on.
- 10-04-2012, 09:02 PM #7
Member
- Join Date
- Oct 2012
- Posts
- 15
- Rep Power
- 0
Re: A little help with a NullPointerException?
java.lang.NullPointerException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(method.java:597)
at edu.rice.cs.drjava.model.compiler.JavaCompiler.run Command(JavaCompiler.java:272)
That's what I get, but I don't know how to interpret it. The code posted is just missing ten comment lines.
- 10-04-2012, 10:07 PM #8
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 19
Re: A little help with a NullPointerException?
Thanks for posting the details.
I threw your code into Eclipse and it compiles fine. The problem is that it won't run. That's because you haven't defined the main() method correctly. It should be:
Java Code:public static void main(String [] args) {
---
In passing, the error message you got from this DrJava thing is not very useful. (to you I mean) If you haven't done so I would strongly suggest you learn to compile and run programs (that's two distinct operations) from the command line (aka "console"). Details are at the "Hello World!" for Microsoft Windows introductory page of Oracle's Tutorial or the near by page for Linux.
I know the last thing you want to hear as you wrestle with the NullPointerException etc is that your compiler sucks. And you may be required to use it for whatever reason. But, in this case, I really do recommend you use the standard technologies perhaps in addition to DrJava.
---
Once you make that change, you'll get a bunch of other compiler messages! That's because the variables in the main() method have to be declared. Remember that until you say "...=new CarV5(...)" that there are no car instances, so the variables you use are not the instance variables you declare at the start of the class. They may have the same name, but they are different variables.
Java Code:public static void main(String [] args) { // local variables - these are declared: String, int etc String carType1 = "2008 Volkswagen Jetta"; int startMiles1 = 81730; // ...
---
Make the two changes suggested ("static" and declare the local variables in main()) and post back if you still have problems with the code you are using and the compiler or runtime messages.
Good luck.
- 10-05-2012, 12:28 AM #9
Member
- Join Date
- Oct 2012
- Posts
- 15
- Rep Power
- 0
Similar Threads
-
NullPointerException
By mangesh.gho in forum New To JavaReplies: 11Last Post: 01-21-2012, 06:19 AM -
NullPointerException
By Diz in forum New To JavaReplies: 10Last Post: 05-13-2011, 02:58 AM -
NullPointerException
By jayragz in forum NetBeansReplies: 5Last Post: 05-12-2011, 05:19 PM -
NullPointerException help me!
By phancuong87 in forum New To JavaReplies: 4Last Post: 01-19-2010, 04:01 PM -
Why do I get a NullPointerException?
By nessa203 in forum New To JavaReplies: 2Last Post: 01-12-2010, 04:02 PM
Bookmarks