This week's book giveaway is in the Java in General forum.
We're giving away four copies of Event Streams in Action and have Alexander Dean & Valentin Crettaz on-line!
See this thread for details.
Win a copy of Event Streams in Action this week in the Java in General forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

NullPointerException - don't know why

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello,
I am writing a program of a library.
I have a class of magazine that contains an arrayList of papers and looks like that:


I also have this method in the class:


I have a library class with this method:


And this are the releveant lines from main:


I have the NullPointerException in the lines: magazinePapers = papers.toString(); and: MyFileLogWriter.writeToFileInSeparateLine(LibraryS ys.getMagazinePapers(magazine).toString());
I would love to know why I am getting this exception.
 
Marshal
Posts: 65037
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Please print out all the object references to the right of the = operator in the line where the exception is thrown. Just before that line insert something likeYes, the %b tag will correctly give you the output you want. Why are you initialising papers in line 4 and reassigning it in line 5?
 
Daniella Levin
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I added prints and I faund out that the problem is with the addMagazine method.
This is the method:


I don't understand what is the problem with it.
 
Saloon Keeper
Posts: 6039
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you say "it has a problem", what exactly is the problem? Does it throw an exception? Unless 'magazines' is null, I don't see a problem here and I wonder how you came to determine that this method is the culprit.
 
Daniella Levin
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here are all the prints I have:


And this is the output:
papers.0 is not null: true
papers.1 is not null: true
papers.2 is not null: true
papers.3 is not null: true
papers.4 is not null: true
papers.5 is not null: true
papers.6 is not null: true
Exception in thread "main" java.lang.NullPointerException
at Model.Test.main(Test.java:34)

The line that has the eror is: if (LibrarySys.addMagazine(magazine)){
This is why I think the problem is in the addMagazine method
 
Carey Brown
Saloon Keeper
Posts: 6039
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm still looking, but this is also a problem...
String magazineName = input.next()
I think you want to call input.nextLine() instead because magazines often have spaces in their names.
 
Rancher
Posts: 4177
47
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Daniella Levin wrote:
The line that has the eror is: if (LibrarySys.addMagazine(magazine)){
This is why I think the problem is in the addMagazine method



If that line is the source of the exception then LibrarySys is null (and is not a class name as I thought originally).

This is why, when you get an exception, it is good to provide the full exception, including the stacktrace, and highlight the line on which it occurs.
 
Carey Brown
Saloon Keeper
Posts: 6039
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dave Tolls wrote:

Daniella Levin wrote:
The line that has the eror is: if (LibrarySys.addMagazine(magazine)){
This is why I think the problem is in the addMagazine method



If that line is the source of the exception then LibrarySys is null (and is not a class name as I thought originally).

This is why, when you get an exception, it is good to provide the full exception, including the stacktrace, and highlight the line on which it occurs.


And it's also a good idea to post a listing with correct line numbers so that line numbers in the stack trace match.
 
Daniella Levin
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The magazine name the program calls is withaut spaces. I tries also changing it to nextLine but I have the same mistake still
 
Carey Brown
Saloon Keeper
Posts: 6039
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are writing to a log file. Do either of these log messages show up?
 
Daniella Levin
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. It also doesn't print the  System.out.printf("papers.7 is not null: %b%n", magazine); line.
The last thing that is printed is: papers.6 is not null: true
 
Carey Brown
Saloon Keeper
Posts: 6039
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dave Tolls wrote:

Daniella Levin wrote:
The line that has the eror is: if (LibrarySys.addMagazine(magazine)){
This is why I think the problem is in the addMagazine method



If that line is the source of the exception then LibrarySys is null (and is not a class name as I thought originally).

This is why, when you get an exception, it is good to provide the full exception, including the stacktrace, and highlight the line on which it occurs.


Right on. Here it is
 
Daniella Levin
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Library is a singelton class and I declared it this way:


I don't understand why this is null beacuse I also have a method to add a book to the library and this method works.
 
Dave Tolls
Rancher
Posts: 4177
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, presumably at this point in the running it is null.

Without seeing more code it'll be hard to say.

I will say one thing.
This (from your stack trace) implies a large amount of your code is running in the main() method:
at Model.Test.main(Test.java:34)
 
Carey Brown
Saloon Keeper
Posts: 6039
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you had used proper java naming conventions we would have found this sooner. Variable names must start with a lower case letter.
 
Campbell Ritchie
Marshal
Posts: 65037
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OP: please read this.
 
Bras cause cancer. And tiny ads:
Java Code Review and Psychology
https://coderanch.com/t/714798/java/Java-Code-Review-Psychology
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!