简体   繁体   English

使用printwriter编辑文件

[英]editing a file with using printwriter

when I run this code for the first time i have no problem but when run it again with already exist file and add another data in this file i see this exception 当我第一次运行此代码时,我没有问题,但是当使用已经存在的文件再次运行它并在该文件中添加另一个数据时,我看到此异常

Exception in thread "main" java.lang.NullPointerException
    at Main.main(Main.java:67)

this is line 67 这是第67行

writer.println(proc[j].ID+"\t"+proc[j].name+"\t"+proc[j].Parent+"\t"+proc[j].state);

this is my code 这是我的代码

import java.util.Scanner;
    import java.io.*;
    public class Main {
        public static void main(String [] args) throws IOException{
            File pro =new File("process.txt");
            Scanner input = new Scanner (System.in);
            int [] ID_Container = new int[10];
            Pro [] proc = new Pro [10] ;
            int memory =1000;
            System.out.println("Enter ID , Name , Parent, State");
            boolean flag =true;

            int i =0;
            while(flag&&i<10){
                System.out.println("please enter the ID");
                int ID=input.nextInt();

                System.out.println("please enter the name");
                String name=input.next();

                System.out.println("please enter the parent");
                int Parent=input.nextInt();

                System.out.println("please enter the State");
                char state=input.next().charAt(0);

                System.out.println("please enter the size");
                int size=input.nextInt();

                if(ID<0||ID>10||ID_Container[ID-1]==1){
                    for (int j = 0; j < 10; j++) {
                        if(ID_Container[j]==0){
                            ID=j+1;
                            System.out.println("Your Id has changed to "+ ID);
                            break;
                        }
                    }
                }

                if(size<memory){
                    proc[i]=new Pro(ID,name,Parent,state,size);
                    ID_Container[ID-1]=1;
                    memory-=size;
                    System.out.println("Do you want to add another process\n1-yes 0-no");
                    int s =input.nextInt();

                    if(s==0)
                        flag= false;
                    i++;
                }
            }
        PrintWriter writer = new PrintWriter(new FileWriter(pro, true));
        writer.println("ID\tname\tparent\tstate");
            for (int j = 0; j < 10; j++) {
                if(ID_Container[j]==0)
                    continue;
                writer.println(proc[j].ID+"\t"+proc[j].name+"\t"+proc[j].Parent+"\t"+proc[j].state);
            }
        writer.close();
        }
    }

    public class Pro {
    int ID;
    String name;
    int Parent;
    char state;
    int size;
    Pro(int ID , String name , int Parent , char state,int size){
        this.ID=ID;
        this.name=name;
        this.Parent=Parent;
        this.state=state;
        this.size=size;
    }
}

由于此时会引发异常,因此对于至少一个特定的索引,数组必须包含null

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM