0

I made a function that is reading a file and putting each line in an array of size 8 which is then adding that array to an arraylist. I am then taking each value per line and trying to pass them into functions but for some reason I am getting null pointers exceptions. Is it because I am using an arraylist? The functions themselves are working if I pass the parameters through the console. I am trying to take the Strings and pass them to a function. example: dep(eachLine.get(0)[1]) does not work. I even created get functions for each slot and put them in a loop with a nested loop to try and pull out the values and it's not working.

public void readFile(String x) throws IOException{
    File file = new File(x);
    BufferedReader br = new BufferedReader(new FileReader(file));
    String str="";

    while((str = br.readLine())!= null)
    {
        String line[] = new String[8];
        int count =0;
        StringTokenizer token = new StringTokenizer(str,",");
        while(token.hasMoreTokens())
        {
            line[count]= token.nextToken();
            count++;
        }

        eachLine.add(line);
    }
}

public int getAccountInt(int i){
    //readFile(x);
    return Integer.parseInt(eachLine.get(i)[4]);
}
public String getName(int i){
    //readFile(x);
    return eachLine.get(i)[1];
}
public int getSSNUM(int i){
    //readFile(x);
    return Integer.parseInt(eachLine.get(i)[2]);
}
public String getAddress(int i){
    //readFile(x);
    return eachLine.get(i)[3];
}
public int getCCAccountInt(int i){
    //readFile(x);
    return Integer.parseInt(eachLine.get(i)[6]);
}
public double getDepositInt(int i){
    //readFile(x);
    return Double.parseDouble(eachLine.get(i)[5]);
}
public double getCreditInt(int i){
    //readFile(x);
    return Double.parseDouble(eachLine.get(i)[7]);
}
public String getCommand(int i){
    //readFile(x);
    return eachLine.get(i)[0];
}

this is in main

x.readFile("today.txt"); System.out.println(x.eachLine.get(0)[4]);

for(int i =0;i<x.eachLine.size();i++){


        String f = x.getCommand(i);
        String s = x.getName(i);
        int a = x.getSSNUM(i);
        String t = x.getAddress(i);
        int b = x.getAccountInt(i);
        double d = x.getDepositInt(i);
        int c = x.getCCAccountInt(i);
        double e = x.getCreditInt(i);

        if(f.equals("ONA")){

        db.createAll(s, a, t, b, d, c, e);


    }
}
Rakesh
  • 4,004
  • 2
  • 19
  • 31
  • 1
    can you post your code here? – Viacheslav Shalamov Mar 20 '19 at 09:29
  • Welcome to SO! Your problem is somewhere else most likely. However, we can't really help you if you don't post any code. – Amongalen Mar 20 '19 at 09:31
  • Which line exactly throws the NPE? And what is `eachLine`? And do you by any chance try to parse a CSV file? –  Mar 20 '19 at 09:35
  • no its a text file. no this is the function i made to put everything in an arraylist. eachLine is the arraylist – NickDaGreek Mar 20 '19 at 09:39
  • Instead of calling getSize(0) use eachLine.get(0)[1] – Jaspreet Jolly Mar 20 '19 at 09:40
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Joakim Danielson Mar 20 '19 at 09:41
  • i typed getSize by accident. – NickDaGreek Mar 20 '19 at 09:43
  • @NickDaGreek could you add to your post the part of code that is relevant to your question : Where you call a function using your arraylist as parameter – vincrichaud Mar 20 '19 at 10:05
  • If there is no token to fill all the 8 locations in the array, of course there will be empty slots in array which are null. So when you try to obtain element like eachLine.get(0)[1], eachLine.get(0) will get the array with size 8 and if that array's second(i.e. [1]) element is null,you will get null pointer exception. – Y.Kakdas Mar 20 '19 at 10:06
  • @Y.Kakdas i filled all 8 indexes and i am testing it with numbers and i am parsing the numbers from strings to integers, but when i just put an integer from the console the function works properly. if i try and pull the number from the array i get a nullpointer to the function, not the array. – NickDaGreek Mar 20 '19 at 10:14
  • The code you've shown so far is not enough to fix your problem. `readFile` method looks to be fine, the problem is somewhere else most likely. Show us how you're using that method and where exactly you're getting the nullpointer. – Amongalen Mar 20 '19 at 10:20
  • @NickDaGreek Where do you create the arraylist? – Y.Kakdas Mar 20 '19 at 10:30
  • @Y.Kakdas in the class, the arraylist has all the correct values. they are just not getting passed to the function. i dont think this is a nullpointer problem – NickDaGreek Mar 20 '19 at 10:32
  • @Amongalen i just edited the post. these are the only functions that are not working. the null pointer is happening because nothing is getting passed to the db.createAll method. – NickDaGreek Mar 20 '19 at 10:35
  • I think you have done this already but just in case I want to ask because I could not find any mistakes. Are you sure that you call the readFile method before to use those functions? – Y.Kakdas Mar 20 '19 at 10:43
  • @Y.Kakdas yes, i changed the code and now i am getting numberformatexception but the line its pointing at is a number. so i dont understand whats going on – NickDaGreek Mar 20 '19 at 10:51
  • @NickDaGreek,please check your parseInt lines,if you try to parse double value with parseInt,it will throw this exception,for example "10.0" is obviously number,can be used as int as well but parseInt will throw exception with pointed numbers. – Y.Kakdas Mar 20 '19 at 11:01

0 Answers0