简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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