This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Exception in thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Exception in thread "main" java.lang.NullPointerException" Watch "Exception in thread "main" java.lang.NullPointerException" New topic
Author

Exception in thread "main" java.lang.NullPointerException

Asha Dore
Greenhorn

Joined: Jul 13, 2011
Posts: 10


This compiles fine, but when I run the tester:


I get these errors:at Weekdays.findDayAL(Weekdays.java:66)
at WeekdaysTester.main(WeekdaysTester.java:11)
Zachary Pepin
Greenhorn

Joined: Jul 29, 2011
Posts: 5
Hey, Asha. This problem is one of the reasons I suggest using an editor with syntax highlighting as it makes mistakes like this slightly more obvious. What has happened here is this:

You have created a field in the Weekdays class called dayNamesAL on line 7, but you have also created a local variable called dayNamesAL on line 17 by placing a type in front of the variables name. Now, Java does allow this, but unfortunately, the rest of the code in your constructor is initializing this local variable and not the field, so when findDayAL is called, the dayNamesAL field is still uninitialized resulting in the NullPointerException.

To correct this, simply remove the type information from in front of the variable name on line 17. That line should look like this.

Asha Dore
Greenhorn

Joined: Jul 13, 2011
Posts: 10
Thank you so much!
Luigi Plinge
Ranch Hand

Joined: Jan 06, 2011
Posts: 441

OP, what you're doing here is really simple... don't you feel a little bit bad taking 80 lines of code? Think of someone trying to maintain it!

You can start by eliminating your constructor and initializing the fields in place, then check out the indexOf method on ArrayList, so you'll end up with something like:
True, in theory the findDayA method isn't quite as efficient as yours since it'll create a new ArrayList each time, but you should write the simplest code that will work, rather than trying to solve a performance problem that doesn't actually exist ("premature optimization").
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37941
    
  22
A lot shorter, but don't encourage beginners to dispense with the constructor.
Asha Dore
Greenhorn

Joined: Jul 13, 2011
Posts: 10
Thanks for the tip! Part of the requirements for this project was to have the constructor, but any information is very useful in the long run!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exception in thread "main" java.lang.NullPointerException
 
Similar Threads
Printing Arrays
Compiler: "This method must return a result of type int" What to do?
identifier expected error
Need Help Stuck :(
if statement (using BlueJ) and equalsIgnoreCase