A friendly place for programming greenhorns!
Big Moose Saloon
Search
|
Java FAQ
|
Recent Topics
Register / Login
JavaRanch
»
Java Forums
»
Java
»
Beginning Java
Author
NullPointerException
Lio Liov
Ranch Hand
Joined: Mar 21, 2012
Posts: 33
posted
Mar 21, 2012 14:44:12
0
I have null pointer exception and My code ,could you help fixing it
Thanks
import java.io.*; import java.util.*; public class FileReader { String fileToOpen; public FileReader(String openFile) { fileToOpen = "jobs/" + openFile + ".txt"; } private Scanner x; public void openFile() { try { x = new Scanner(new File(fileToOpen)); } catch (Exception e) { System.out.println("Could not find the job"); } } public String[] readFile() { String[] values = null; int i = 0; try { while (x.hasNext()) { String line = x.nextLine(); String edno = line.substring(line.indexOf(':') + 1); values[i] = edno; i++; } } catch (Exception e) { } return values; } public static void main(String[] args) { String job = "SecondJob"; FileReader inp = new FileReader(job); inp.openFile(); System.out.println(inp.readFile().length); } }
Koen Aerts
Ranch Hand
Joined: Feb 07, 2012
Posts: 344
I like...
posted
Mar 21, 2012 14:46:17
0
Stacktrace would help.
dennis deems
Ranch Hand
Joined: Mar 12, 2011
Posts: 808
posted
Mar 21, 2012 14:59:34
0
The
String
array is never instantiated. It is explicitly set to null, and is still null when you try to write a String to one of its indices.
Lio Liov
Ranch Hand
Joined: Mar 21, 2012
Posts: 33
posted
Mar 21, 2012 15:04:32
0
Thanks ,can I instantiate the array without specified the size
Stefaan Dutry
Ranch Hand
Joined: Sep 17, 2010
Posts: 32
I like...
posted
Mar 21, 2012 16:14:57
0
Lio Liov wrote:
can I instantiate the array without specified the size
I would suggest looking at, and using
ArrayList
.
Especialy it's
add
method and it's
toArray
method.
Although i can't think of a reason to convert it to an Array again in this case.
I agree. Here's the link:
http://ej-technologies/jprofiler
- if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
subject: NullPointerException
Similar Threads
When the Statment After Finally clause will get Executed
Giving runtime error
How To Read A Web Page
newbie question about servlets calling methods
Scanner class
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter