Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java ArrayList在写入文件时变为null_Java_Arraylist_Nullpointerexception_Writer - Fatal编程技术网

Java ArrayList在写入文件时变为null

Java ArrayList在写入文件时变为null,java,arraylist,nullpointerexception,writer,Java,Arraylist,Nullpointerexception,Writer,尝试写入文件后,我的arraylist变为null WriteToFile类arraylist信息在执行最后一行后变为null writer.write(info.get(i).getIpadd().toString()+"\n"); 当我将另一个列表写入文件时,它在第一个实例上工作,但在第二个实例上运行时不工作。我不明白为什么会这样。下面是整个代码和堆栈跟踪 WriteToFile类: public class WriteToFile { public WriteToFile(Ar

尝试写入文件后,我的arraylist变为null

WriteToFile类arraylist信息在执行最后一行后变为null

    writer.write(info.get(i).getIpadd().toString()+"\n");
当我将另一个列表写入文件时,它在第一个实例上工作,但在第二个实例上运行时不工作。我不明白为什么会这样。下面是整个代码和堆栈跟踪

WriteToFile类:

public class WriteToFile {

public WriteToFile(ArrayList<Information> info,String location) 
{
    FileWriter writer=null;
    try
    {
        writer = new FileWriter(location); 
        System.out.println(info.size());
        for(int i=0;i<info.size()-1;i++) 
        {
          writer.write(info.get(i).getDate().toString()+",");
          writer.write(info.get(i).getAccount().toString()+",");
          writer.write(info.get(i).getStatus().toString()+",");
          writer.write(info.get(i).getIpadd().toString()+"\n");
          System.out.println(info.get(i).getAccount());
        }
    }
    catch(Exception e)
    {
        e.printStackTrace();
        System.out.println(e.getMessage());
    }
    finally
    {
        try {
            writer.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

}
ReaderFromLog类

public class ReaderFromLog {


Scanner s1 = null;
String line=null;
ArrayList<Information> log;

public ReaderFromLog(String logFileLocation) {
    // TODO Auto-generated constructor stub
    File logFile = new File(logFileLocation);

    if(!logFile.exists())
    {
        System.err.println("File not found");
        System.exit(1);
    }
    else
    {
        try 
        {
            s1 = new Scanner(new FileReader(logFile));
        } catch (FileNotFoundException e) 
        {
            System.err.print("File not found");
        }
    }

    log=new ArrayList<Information>();
    //store into a array
    //exclude any repeats


    do{
        line=s1.nextLine();
        Information newUser = new Information();
        if(line.contains("Access-Accept for user"))
        {
            newUser.setStatus("Accept");
            String[] sb=line.split(" ");
            newUser.setAccount(sb[7]);
            int idx_Ipadd = 0;
            for(int i=0;i<sb.length;i++)
                if (sb[i].contentEquals("to"))
                    idx_Ipadd=i;
            newUser.setIpadd(sb[idx_Ipadd+1]+ " " + sb[idx_Ipadd+2]);
            newUser.setDate(sb[0]+ " "+sb[1] + " " +sb[2]+" " + sb[3].substring(0, 4));
            log.add(newUser);
        }
        else if(line.contains("Access-Reject for user"))
        {
            newUser.setStatus("Reject");
            String[] sb=line.split(" ");
            newUser.setAccount(sb[7]);
            int idx_Ipadd = 0;
            for(int i=0;i<sb.length;i++)
                if (sb[i].contentEquals("to"))
                    idx_Ipadd=i;
            newUser.setIpadd(sb[idx_Ipadd+1]+ " " + sb[idx_Ipadd+2]);
            newUser.setDate(sb[0]+ " "+sb[1] + " " +sb[2]+" " + sb[3].substring(0, 4));
            log.add(newUser);
        }
    }while(s1.hasNextLine());
}

}
公共类ReaderFromLog{
扫描程序s1=空;
字符串行=null;
阵列列表日志;
公共ReaderFromLog(字符串logFileLocation){
//TODO自动生成的构造函数存根
文件logFile=新文件(logFileLocation);
如果(!logFile.exists())
{
System.err.println(“未找到文件”);
系统出口(1);
}
其他的
{
尝试
{
s1=新扫描仪(新文件读取器(日志文件));
}catch(filenotfounde异常)
{
System.err.print(“未找到文件”);
}
}
log=新的ArrayList();
//存储到数组中
//排除任何重复
做{
line=s1.nextLine();
信息newUser=新信息();
if(第行包含(“用户访问接受”))
{
newUser.setStatus(“接受”);
字符串[]sb=行。拆分(“”);
newUser.setAccount(sb[7]);
int idx_Ipadd=0;
对于(int i=0;i检查

info.get(i).getIpadd()

如果该值为空,那么.toString(0)将给您NullPointerException

它有一个值。谢谢,伙计。我只是重新编辑了我的代码。将打印到文件类与其他类合并。它成功了。我感到困惑
public class Information {

private String ipadd;
private String status;
private String account;
private String date;


public String getIpadd() {
    return ipadd;
}

public void setIpadd(String ipadd) {
    this.ipadd = ipadd;
}

public String getStatus() {
    return status;
}

public void setStatus(String status) {
    this.status = status;
}

public String getAccount() {
    return account;
}

public void setAccount(String account) {
    this.account = account;
}

public String getDate() {
    return date;
}

public void setDate(String date) {
    this.date = date;
}
}
public class ReaderFromLog {


Scanner s1 = null;
String line=null;
ArrayList<Information> log;

public ReaderFromLog(String logFileLocation) {
    // TODO Auto-generated constructor stub
    File logFile = new File(logFileLocation);

    if(!logFile.exists())
    {
        System.err.println("File not found");
        System.exit(1);
    }
    else
    {
        try 
        {
            s1 = new Scanner(new FileReader(logFile));
        } catch (FileNotFoundException e) 
        {
            System.err.print("File not found");
        }
    }

    log=new ArrayList<Information>();
    //store into a array
    //exclude any repeats


    do{
        line=s1.nextLine();
        Information newUser = new Information();
        if(line.contains("Access-Accept for user"))
        {
            newUser.setStatus("Accept");
            String[] sb=line.split(" ");
            newUser.setAccount(sb[7]);
            int idx_Ipadd = 0;
            for(int i=0;i<sb.length;i++)
                if (sb[i].contentEquals("to"))
                    idx_Ipadd=i;
            newUser.setIpadd(sb[idx_Ipadd+1]+ " " + sb[idx_Ipadd+2]);
            newUser.setDate(sb[0]+ " "+sb[1] + " " +sb[2]+" " + sb[3].substring(0, 4));
            log.add(newUser);
        }
        else if(line.contains("Access-Reject for user"))
        {
            newUser.setStatus("Reject");
            String[] sb=line.split(" ");
            newUser.setAccount(sb[7]);
            int idx_Ipadd = 0;
            for(int i=0;i<sb.length;i++)
                if (sb[i].contentEquals("to"))
                    idx_Ipadd=i;
            newUser.setIpadd(sb[idx_Ipadd+1]+ " " + sb[idx_Ipadd+2]);
            newUser.setDate(sb[0]+ " "+sb[1] + " " +sb[2]+" " + sb[3].substring(0, 4));
            log.add(newUser);
        }
    }while(s1.hasNextLine());
}

}
public class RemoveDuplicatesInList {

Scanner s1 = null;
String line=null;
ArrayList<Information> log;

public RemoveDuplicatesInList(String duplicateFileLocation) 
{
    // TODO Auto-generated constructor stub
    File logFile = new File(duplicateFileLocation);

    if(!logFile.exists())
    {
        System.err.println("File not found");
        System.exit(1);
    }
    else
    {
        try 
        {
            s1 = new Scanner(new FileReader(logFile));
        } catch (FileNotFoundException e) 
        {
            System.err.print("File not found");
        }
    }

    log=new ArrayList<Information>();
    //store into a array
    //exclude any repeats
    do{
        boolean sameAccount=false;
        line=s1.nextLine();
        Information newUser = new Information();
        if(line.contains("Accept"))
        {
            newUser.setStatus("Accept");
            String[] sb=line.split(",");
            sameAccount=false;
            for(int i=0;i<log.size();i++)
                if(log.get(i).getAccount().contentEquals(sb[1]))
                {
                    sameAccount=true;
                    break;
                }
            if(!sameAccount)
            {
                newUser.setAccount(sb[1]);
                newUser.setIpadd(sb[3]);
                newUser.setDate(sb[0]);
                log.add(newUser);
            }

        }
        else if(line.contains("Reject"))
        {
            newUser.setStatus("Reject");
            String[] sb=line.split(",");
            for(int i=0;i<log.size();i++)
                if(log.get(i).getAccount().contentEquals(sb[1]))
                {
                    sameAccount=true;
                    break;
                }
            if(!sameAccount)
            {
                newUser.setAccount(sb[1]);
                newUser.setIpadd(sb[3]);
                newUser.setDate(sb[0]);
                log.add(newUser);
            }
        }
    }while(s1.hasNextLine());
}

}
info.get(i).getIpadd()