-2

This is my code example and all seems to be working with inputting data, however when trying to export the input data to an output the code is returning the error:

"Exception in thread "main" java.lang.NullPointerException at JavaProject.main(JavaProject.java:50)"

I have included a gyazo snapshot of how it looks and the error:

import java.util.Scanner;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;

public class JavaProject {

    private static char[] input;

    @SuppressWarnings("null")
    public static void main(String[] args) {

        int hrs, mins;
        int[] gameCount;
        int[] MinutesPlayed = null;
        String GamerName, GamerReport;

        //Main data storage arrays
        String[] GameNames = new String[100];
        int[] HighScores = new int[100];


        Scanner Scan = new Scanner(System.in);

        //formatting for output and input
        System.out.println("////// Game Score Report Generator \\\\\\\\\\\\");
        System.out.println("     ");

        //user enters name and then moves to next line
        System.out.println("Enter Your Name");
        GamerName = Scan.nextLine();

        //user is given an example of input format 
        System.out.println("Input Gamer Information " + "Using Format --> Game : Achievement Score : Minutes Played");
        System.out.println("    ");


        System.out.println("Game : Achievement Score : Minutes Played");
        GamerReport = Scan.nextLine();


        String[] splitUpReport; // an array of string
        splitUpReport = GamerReport.split(":"); // split the text up on the colon

        int i = 1;

        //copy data from split text into main data storage arrays 
        GameNames[i] = splitUpReport[0];
        HighScores[i] = Integer.parseInt(splitUpReport[1].trim() );
        MinutesPlayed[i] = Integer.parseInt(splitUpReport[2].trim());

        //output to file 

        try
        {
            FileWriter writer = new FileWriter("output.txt");
            writer.write(GamerReport);
            writer.close();
        } catch (IOException e)
        {
            System.err.println("File does not exist!");
        }
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • As a side note, java variables should be in camelCase. Its hard to read your code because `MinutesPlayed` format etc are supposed to be for class names. – reversebind Jan 06 '16 at 00:03

2 Answers2

0

this is never initialized int[] MinutesPlayed = null;

reversebind
  • 1,216
  • 1
  • 14
  • 18
0

This line:

MinutesPlayed[i] = Integer.parseInt(splitUpReport[2].trim());

Gives you java.lang.NullPointerException because the array doesn't initialized try something like this:

 int[] MinutesPlayed = new int[100];

Instead of int[] MinutesPlayed = null;

Abdelhak
  • 8,299
  • 4
  • 22
  • 36