• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Exception in thread "main" java.lang.NullPointerException

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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)
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you so much!
 
Ranch Hand
Posts: 441
Scala IntelliJ IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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").
 
Marshal
Posts: 79716
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A lot shorter, but don't encourage beginners to dispense with the constructor.
 
Asha Dore
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Did Steve tell you that? Fuh - Steve. Just look at this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic