0

I'm trying tackle this problem for a few days now but with no success. Here is the code:

import java.util.*;
import java.io.*;
public class Portefeuille {
private ArrayList<Woning> woningen;

public Portefeuille(){
    woningen = new ArrayList<Woning>();
}

public void voegToe(Woning w){
    if(woningen.contains(w)==false)
        woningen.add(w);
    else
        System.out.println(w.toString()+" komt al voor en is daarom niet toegevoegd.");
}

public ArrayList<Woning> woningenTot(int maxprijs){
    ArrayList<Woning> totaal = new ArrayList<Woning>();
    for(int i=0; i<woningen.size(); i++){
        if((woningen.get(i)).KostHooguit(maxprijs))
            totaal.add(woningen.get(i));
    }
    return totaal;
}

public static Portefeuille read(String infile){
    Portefeuille woningen = new Portefeuille();
    try
    {
        FileReader file = new FileReader(infile);
        Scanner sc = new Scanner(file);
        int aantalwoningen = sc.nextInt();
        for(int i=0; i<aantalwoningen; i++){
            Woning woning = Woning.read(sc);
            woningen.voegToe(woning);
        }
        System.out.println(woningen.toString());
        sc.close();
    } catch(Exception e){
        System.out.println(e);
      }
 return null;
 }
}

And here is the main file

    import java.util.*;
    public class Test2 {
public static void main(String[] args){

    Portefeuille bestand = Portefeuille.read("in.txt");
    ArrayList<Woning> WTot = bestand.woningenTot(21500);

}
}

The error i am getting: Exception in thread "main" java.lang.NullPointerException at Test2.main(Test2.java:6)

I would really appreciate if someone could just at least point me in the right direction.

Thanks,

Jaspreet

1
  • What the..? Can you please write above each code whats the file and also what is "KostHooguit" in (woningen.get(i)).KostHooguit(maxprijs)? And please full stack trace Commented Dec 8, 2011 at 13:01

5 Answers 5

2

You'll get a NullPointerException when you end up trying to call a method on a reference that points to null, rather than an object. In your case, that would be bestand.woningenTot(21500); because the call to Portefeuille.read("in.txt"); always returns null.

Sign up to request clarification or add additional context in comments.

Comments

1

Your Portefeuille.read("in.txt") returns null instead of woningen.

    public static Portefeuille read(String infile){
        Portefeuille woningen = new Portefeuille();
        try
        {
            FileReader file = new FileReader(infile);
            Scanner sc = new Scanner(file);
            int aantalwoningen = sc.nextInt();
            for(int i=0; i<aantalwoningen; i++){
                Woning woning = Woning.read(sc);
                woningen.voegToe(woning);
            }
            System.out.println(woningen.toString());
            sc.close();
        } catch(Exception e){
            System.out.println(e);
          }
     return woningen ;
     }
    }

Comments

1

you return null in your static read function... So you cant acces the object in line6. Try to return woningen instead.

Comments

1

The method Portefeuille.read always returns null. You need to return the Portefeuille you are creating.

Side comments: - Call close always in a finally section - Use enhanced looks instead of normal for loops. Like for(String s: collectionOfStrings) - Try to program using interfaces instead of concrete classes if possible. E.g.: use List instead of ArrayList

Comments

0

Well it looks to me like

public static Portefeuille read(String infile) 

is always returning null. Perhaps there should be a

return woningen;

after sc.close();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.