wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes NullPointerException on what looks like a good array 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 "NullPointerException on what looks like a good array" Watch "NullPointerException on what looks like a good array" New topic
Author

NullPointerException on what looks like a good array

Zach Burnham
Greenhorn

Joined: Feb 15, 2006
Posts: 16
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

Joined: Feb 07, 2005
Posts: 2367
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
Sheriff

Joined: Sep 28, 2004
Posts: 18508
    
  40


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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Zach Burnham
Greenhorn

Joined: Feb 15, 2006
Posts: 16
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

Joined: Aug 31, 2004
Posts: 11343

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 ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
 
 
subject: NullPointerException on what looks like a good array
 
Similar Threads
Array of Hashmaps
Reading dir
How to delete all existing files in a dierectry?
NullPointerException - but why?