Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

NullPointerException on what looks like a good array

 
Zach Burnham
Greenhorn
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following code throws NullPointerException on line 5; when I comment out 5, it throws it on 6. All the classes referred to should be valid.



I don't see the issue , anyone got any insight?

Z
 
Keith Lynn
Ranch Hand
Posts: 2399
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Recall that when you create an array of object references, the initial value of those references is null. You have to set an individual reference in the array to point to an actual object before you call methods on it.
 
Henry Wong
author
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

processDirectory.myFile[] myFiles = new processDirectory.myFile[2];
myFiles[0].filename = "Foojpg";
myFiles[1].filename = "Bartxt";


You created the array -- but did not create the members. Hence, when you tried to access the filename variable, you'll get a null pointer exception.

Henry
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Otto,

Welcome to JavaRanch!

First, a bit of business: you may not have read our naming policy on the way in. It requires that you use a full, real (sounding) first and last name for your display name. Joke names and "handles" don't work at the Ranch. You can change your display name here. Thanks for your immediate attention.
 
Zach Burnham
Greenhorn
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help guys, I think that'll take care of it.

Is there a quick way to populate an array with objects without doing a for() loop?

I'll go and change the name.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Evil Otto:
... Is there a quick way to populate an array with objects without doing a for() loop? ...

The fill methods in java.util.Arrays might work for you, depending on what exactly you need. The drawback to this method is that each index references the same object.
[ February 16, 2006: Message edited by: marc weber ]
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic