Java IO & String manipulation
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Albert_Leng
Posted On:   Tuesday, February 5, 2002 01:47 PM

Hi there. I'm a java programming novice and learning to read from text file and use the data in the text file to display some images. I think that i have made some silly mistakes. I can compile the program but when i try to run it, the following message appears in the console: ... java.lang.NullPointerException at LightbarPanel1. (LightbarPanel1.java:41) ... Can you please have a look of the pieces of my code below and tell me what's wrong with it? ... String line1, line2, line3, file1 ="lenses1.txt", file2 ="chassis.txt", file3="rotator.txt"; String[] endLeftName,endLeftFName, endLeftChassisName, endLeftChassisFName; String[] endRightName, e   More>>

Hi there. I'm a java programming novice and learning to read from text file and use the data in the text file to display some images. I think that i have made some silly mistakes. I can compile the program but when i try to run it, the following message appears in the console:

			
... java.lang.NullPointerException
at LightbarPanel1. (LightbarPanel1.java:41)
...

Can you please have a look of the pieces of my code below and tell me what's wrong with it?

...

String line1, line2, line3, file1 ="lenses1.txt", file2 ="chassis.txt", file3="rotator.txt";
String[] endLeftName,endLeftFName, endLeftChassisName, endLeftChassisFName;
String[] endRightName, endRightFName, endRightChassisName, endRightChassisFName;
String[] rotator;
StringTokenizer tokenizer1, tokenizer2, tokenizer3;


public LightbarPanel1(){

MediaTracker tracker = new MediaTracker(this);
try{
FileReader fR1 = new FileReader (file1);
BufferedReader bR1 = new BufferedReader(fR1);




line1 = bR1.readLine();
while(line1 != null)
{ int i=0;
tokenizer1 = new StringTokenizer(line1);
endLeftName[i++] = new String(tokenizer1.nextToken());// line 41
System.out.println(i);
System.out.println(i+1f);
endLeft[i++] = Toolkit.getDefaultToolkit().getImage(endLeftName[i++]);
//endLeftFName[i++] = new String(tokenizer1.nextToken());
//endLeftF = Toolkit.getDefaultToolkit().getImage(endLeftFName[i++]);
endRightName[i++] = new String(tokenizer1.nextToken());
endRight[i++] = Toolkit.getDefaultToolkit().getImage(endRightName[i++]);
endRightFName[i++] = new String(tokenizer1.nextToken());
endRightF[i++] = Toolkit.getDefaultToolkit().getImage(endRightFName[i++]);
line1 = bR1.readLine();


}

bR1.close();
...


Please use html tags to format code blocks.

   <<Less

Re: Java IO & String manipulation

Posted By:   Stephen_Ostermiller  
Posted On:   Wednesday, February 6, 2002 08:03 AM

Each of your String arrays are null because you never instantiate them.
Somewhere in your code you need something like:


endLeftName = new String[30];


You will also need code to check that you are not stepping off the end of the arrays or switch to using Vectors, which are automatically resized.

About | Sitemap | Contact