编程新手,Java.lang.NullPointerException with ArrayList

编程新手,Java.lang.NullPointerException with ArrayList,java,arraylist,nullpointerexception,main,Java,Arraylist,Nullpointerexception,Main,我尝试解决这个问题几天了,但没有成功。 代码如下: 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(wonin

我尝试解决这个问题几天了,但没有成功。 代码如下:

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;
 }
}
import java.util.*;
导入java.io.*;
公共级波特维尔酒店{
私人ArrayList woningen;
公共港口(){
woningen=新的ArrayList();
}
公共空间voegToe(Woning w){
if(woningen.contains(w)=false)
添加(w);
其他的
System.out.println(w.toString()+“komt al voor en is daarom niet toegevogd.”);
}
公共阵列列表woningenTot(int maxprijs){
ArrayList totaal=新的ArrayList();

对于(inti=0;i嗯,在我看来

public static Portefeuille read(String infile) 
总是返回null。也许应该有一个

return woningen;

在sc.close()之后;

在我看来

public static Portefeuille read(String infile) 
总是返回null。也许应该有一个

return woningen;

在sc.close();

之后,当您试图对指向null的引用而不是对象调用一个方法时,您将得到一个NullPointerException。在您的情况下,这将是
bestand.WoningeTot(21500);
因为调用
portefueille.read(“In.txt”)会导致
始终返回
null

当您试图调用指向null而不是对象的引用上的方法时,您将得到一个NullPointerException。在您的情况下,这将是
bestand.woningetot(21500);
因为调用
portefueille.read(“In.txt”)会导致
始终返回
null
您的
portefeille.read(“in.txt”)
返回
null
,而不是
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 ;
     }
    }
公共静态端口读取(字符串填充){
Portefueille woningen=新Portefueille();
尝试
{
FileReader文件=新的FileReader(内嵌);
扫描仪sc=新扫描仪(文件);
int aantalwoningen=sc.nextInt();

for(int i=0;i您的
portefueille.read(“in.txt”)
返回
null
而不是
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 ;
     }
    }
公共静态端口读取(字符串填充){
Portefueille woningen=新Portefueille();
尝试
{
FileReader文件=新的FileReader(内嵌);
扫描仪sc=新扫描仪(文件);
int aantalwoningen=sc.nextInt();

对于(int i=0;i在静态读取函数中返回
null
,因此无法访问第6行中的对象。
尝试返回
woningen

在静态读取函数中返回
null
,因此无法访问第6行中的对象。
尝试返回
woningen

方法portefueille.read始终返回null。您需要返回正在创建的portefueille

旁白: -呼叫结束总是在最后一段 -对循环使用增强外观而不是普通外观。例如(字符串s:collectionOfStrings)
-如果可能,尝试使用接口而不是具体类进行编程。例如:使用列表而不是ArrayList

方法portefeille.read始终返回null。您需要返回正在创建的portefeille

旁白: -呼叫结束总是在最后一段 -对循环使用增强外观而不是普通外观。例如(字符串s:collectionOfStrings)
-如果可能的话,尝试使用接口而不是具体的类进行编程。例如:使用列表而不是ArrayList

什么是..?你能在每个代码上面写下文件是什么,以及(woningen.get(i)).KostHooguit(maxprijs)中的“KostHooguit”是什么吗请在每个代码上面写下文件是什么,以及(woningen.get(i)).KostHooguit(maxprijs)中的“KostHooguit”是什么?并请写下完整的堆栈跟踪