0

so I made a program that's basically a simple address book, ( i'm a new programmer). I have it so it reads the contacts from a text file, and saves changes to it too. my issue is it saves just find if I don't add an element to the array before saving ( basically a new contact) but when i do i get "java.lang.NullPointerException at java.io.Writer.write(Unknown Source)" here's my code

  package adressbook;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.LineNumberReader;
import java.util.Scanner;

public class marks {
    static Scanner scanner = new Scanner(System.in);
    // static String studentinfo[][] = { { "", "", "" }, { "", "", "" }, { "", "",
    // "" }, { "", "", "" } };
    static String studentinfo[][] = new String[4][3];
    static int mark[];
    static File info = new File("D:\\info.txt");
    static int linecount;
    static String input;

    static int selected;

    public static void select() throws IOException {

        System.out.println("who would you like to change?");
        for (int i = 0; i < studentinfo[0].length; i++) {
            System.out.println(i + 1 + "." + studentinfo[0][i]);
        }
        input = scanner.nextLine();

        for (int i = 0; i < studentinfo[0].length; i++) {
            // System.out.println(selected);
            if (input.equals(studentinfo[0][i])) {
                selected = i;

            }
        }

    }

    public static void scan() throws IOException {

        FileReader input = new FileReader(info);
        LineNumberReader count = new LineNumberReader(input);
        String lineRead = "";
        while ((lineRead = count.readLine()) != null) {
            linecount = count.getLineNumber();
        }
        Scanner s = new Scanner(info);
        String[][] resizer = new String[4][linecount / 4];
        studentinfo = resizer;
        for (int i = 0; i < linecount / 4; i++) {

            studentinfo[0][i] = s.nextLine();
            System.out.println(studentinfo[0][i]);
            studentinfo[1][i] = s.nextLine();
            System.out.println(studentinfo[1][i]);
            studentinfo[2][i] = s.nextLine();
            System.out.println(studentinfo[2][i]);
            studentinfo[3][i] = s.nextLine();
            System.out.println(studentinfo[3][i]);

        }

        s.close();
    }

    public static void printout() {
        for (int i = 0; i < studentinfo[0].length; i++) {
            System.out.println(studentinfo[0][i]);
            System.out.println(studentinfo[1][i]);
            System.out.println(studentinfo[2][i]);
            System.out.println(studentinfo[3][i]);

        }
    }

    public static void save() throws IOException {
        scanner.close();
        BufferedWriter writer = new BufferedWriter(new FileWriter(info));
        for (int i = 0; i < studentinfo[0].length; i++) {

            writer.write(studentinfo[0][i]);
            writer.newLine();
            writer.write(studentinfo[1][i]);
            writer.newLine();
            writer.write(studentinfo[2][i]);
            writer.newLine();
            writer.write(studentinfo[3][i]);
            writer.newLine();

        }
        writer.close();
    }

    public static void addnew() throws IOException {
        System.out.println("enter new name:");
        String newname = scanner.next();
        System.out.println("enter new number:");
        String newnum = scanner.next();
        System.out.println("enter new address:");
        String newaddr = scanner.next();
        System.out.println("enter new email:");
        String newemail = scanner.next();
        String temp[][] = new String[4][studentinfo.length + 1];

        for (int i = 0; i == studentinfo.length; i++) {
            temp[0][i] = studentinfo[0][i];
            temp[1][i] = studentinfo[1][i];
            temp[2][i] = studentinfo[2][i];
            temp[3][i] = studentinfo[3][i];

        }
        studentinfo = temp;
        studentinfo[0][studentinfo.length] = newname;
        studentinfo[1][studentinfo.length] = newnum;
        studentinfo[2][studentinfo.length] = newaddr;
        studentinfo[3][studentinfo.length] = newemail;
        System.out.println("new contact added!");

        save();
        // printout();
    }

    public static void main(String[] args) throws IOException {
        scan();// reads in from file
        System.out.println("change info? Type yes or no");
        input = scanner.nextLine();// gets user input
        System.out.println(input);

        if (input.equals("yes")) {
            select();
            System.out.println("name, number, address or email?");
            input = scanner.nextLine();

            if (input.equals("name")) {
                System.out.println("enter new name");
                input = scanner.nextLine();
                studentinfo[0][selected] = input;

            } else if (input.equals("number")) {
                System.out.println("enter new number");
                input = scanner.nextLine();
                studentinfo[1][selected] = input;

            } else if (input.equals("address")) {
                System.out.println("enter new address");
                input = scanner.nextLine();
                studentinfo[2][selected] = input;
            } else if (input.equals("email")) {
                System.out.println("enter new email");
                input = scanner.nextLine();
                studentinfo[3][selected] = input;
            }

            printout();
            System.out.println("save changes?");
            input = scanner.nextLine();
            if (input.equals("yes")) {

                save();
            }

            else {

            }

        }

        else if (input.equals("no")) {
            System.out.println("add new contact? type yes or no");
            input = scanner.next();
            if (input.equals("yes")) {

                addnew();

            }
        }

    }

}
  • 1
    Inspect the line in your code that is throwing the NPE, find out which variable is null, and then check back in the code to see why. – Hovercraft Full Of Eels May 13 '18 at 17:00
  • i dont really know how , but the array gets emptied when i add a new element and that creates a null when it tried to write – Yamaan Bakeer May 13 '18 at 19:12
  • OK, you're getting there -- so what variable *exactly* is null when you try to write? – Hovercraft Full Of Eels May 13 '18 at 19:13
  • actually that theory is incorrect i have something that resizes the array to prevent that – Yamaan Bakeer May 13 '18 at 19:17
  • the array out of bounds happens when i try to write the new addition to the array, i do it like this: studentinfo = temp; studentinfo[0][studentinfo[0].length] = newname; studentinfo[1][studentinfo[0].length] = newnum; studentinfo[2][studentinfo[0].length] = newaddr; studentinfo[3][studentinfo[0].length] = newemail; temp is for resizing the original array – Yamaan Bakeer May 13 '18 at 19:18
  • so i added -1 to .length since i think that uses 1 number above since arrays start at 0, but then the other eliments of my array go null – Yamaan Bakeer May 13 '18 at 19:23
  • i fixed the null pointer exeption, and the array out of bounds, but it shouldnt delete the other elements of my array when i try to add something to the end of it – Yamaan Bakeer May 13 '18 at 19:25

0 Answers0