Exception in thread "main" java.lang.NullPointerException
I am getting this exception in the code below for not initializing arraylist.. could someone please help me with this..:(
this problem is in g[i].v.add(vv);
class graph
{
public ArrayList<Integer[]> v=new ArrayList<Integer[]>();
public ArrayList<Integer[]> e=new ArrayList<Integer[]>();
}
public class main {
public static void main(String[] args) {
Integer vv[]=new Integer[2];
Integer ee[]=new Integer[3];
graph g[]=new graph[1000];
int i=-1;
String t="t";
String v="v";
String e="e";
FileInputStream fstream;
try {
fstream = new FileInputStream("graph1.txt");
DataInputStream in = new DataInputStream(fstream);
BufferedReader b = new BufferedReader(new InputStreamReader(in));
String line;
try {
while ((line = b.readLine()) != null) {
StringTokenizer st = new StringTokenizer(line);
if(t.equals(st.nextToken()))
{
i++;
}
line = b.readLine();
System.out.println(line);
StringTokenizer st1 = new StringTokenizer(line);
if(v.equals(st1.nextToken()))
{
vv[0]=Integer.parseInt(st1.nextToken());
vv[1]=Integer.parseInt(st1.nextToken());
g[i].v.add(vv);
}
line = b.readLine();
StringTokenizer st2 = new StringTokenizer(line);
if(e.equals(st2.nextToken()))
{
ee[0]=Integer.parseInt(st2.nextToken());
ee[1]=Integer.parseInt(st2.nextToken());
ee[2]=Integer.parseInt(st2.nextToken());
g[i].e.add(ee);
}
}
} catch (IOException ex) {
Logger.getLogger(main.class.getName()).log(Level.S EVERE, null, ex);
}
} catch (FileNotFoundException ex) {
Logger.getLogger(main.class.getName()).log(Level.S EVERE, null, ex);
}
}}
Re: Exception in thread "main" java.lang.NullPointerException
What variable has a null value?
Please post the full text of the error message.
Some bad practices I see are:
using variable names that are only 1 character long
having more than one variable with the same name: e & v
You need to fix those.
Also use code tags: http://www.java-forums.org/misc.php?do=bbcode#code
Re: Exception in thread "main" java.lang.NullPointerException
Duplicate of http://www.java-forums.org/new-java/...exception.html
Closing the thread and banning the javagirl24 account.
db
THREAD CLOSED