# Thread: A little help with a NullPointerException?

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

}

}```
Any help with the error?

2. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,712
Rep Power
14

## 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.

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.

4. Senior Member
Join Date
Sep 2012
Posts
108
Rep Power
0

## Re: A little help with a NullPointerException?

post the error

5. Moderator
Join Date
Apr 2009
Posts
13,075
Rep Power
23

## Re: A little help with a NullPointerException?

Originally Posted by ssrock64
The error traces to lines 25 and 39, according to DrJava.
And you need to show us which lines in your code those are, because they do not match the lines in your OP.

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.

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.

8. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,712
Rep Power
14

## 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)
{```
Note the use of "static" when you declare the entry point of a program. That's necessary because when the program starts there is no instance of CarV5 yet (you haven't created one with "new") and only a static method which doesn't belong to any particular instance can kick the whole thing off.

---

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;

// ...```
At this point the programs compiles. But don't open the champaign yet! There's a runtime error from a little typo you made elsewhere.

---

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.

9. Member
Join Date
Oct 2012
Posts
15
Rep Power
0

## Re: A little help with a NullPointerException?

Wow, I just forgot something incredibly basic. That was too obvious.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•