Hi All,
Im working on a program for school which combines 3 programs together(one transposes a 2d array, one adds up the diagonals and tells them, and one adds the rows). The problem Im having, is with importing all the data into the 2 Dimensional array "input" so it can be used by all the methods. Everytime I try to run the program, it gives me"java.lang.NullPointerException: null". How Do I Fix This?

My Code:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
/**
 * This is program combines 3 programs in 1
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class threeprogs
{
    // instance variables - replace the example below with your own
    private int x;
    public static int[][] input;
    public static int[][] output;

    /**
     * Runs All methods
     */
    public  static void main(String[] args)
    {
       //inputs info

        File file = new File("prog464a.dat");
        try { 
            Scanner scanner = new Scanner(file);
            while(scanner.hasNextInt())
            {
                for(int i = 0; i<5; i++)
                {
                    for(int j = 0; j<5; j++)
                    {
                        input[i][j] = scanner.nextInt();
                        output[i][j] = input[i][j];
                    }   
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
       int programs = 0;
       while(programs <3)
        {

            //make all programs execute here
            switch (programs)
            {
                    case 0:  transpose();
                     break;
                     case 1:  AddDiag();
                     break;
                     case 2:  AddRowsandCol();
                     break;
                    }
             //prints output
            for(int i=0; i<5;i++)
            {
                System.out.print("\n");
                for(int j=0;j<5;j++)
                {
                    System.out.print(output[i][j]);
                }

            }
        }
    }

    /**
     * An example of a method - replace this comment with your own
     * 
     * @param  y   a sample parameter for a method
     * @return     the sum of x and y 
     */
    public static int[][] transpose()
    {  

        for(int i=0; i<input.length; i++)
        {
            for(int j=1; j<input[0].length; j++)
            {
                output[i][j] = input[j][i];
            }
        }
        return output;
    }
    public static String AddRowsandCol()
    {  
        int total = 0;
        int loop = 0;
        for(int i=0; i<5; i++)
        {
           for(int j=0; j<5; j++)
           {
               if(loop<i)
               {
                    output[loop][5] = total;
                    total = 0;
               }
               total += output[i][j];
            }
        }

        return "Origional Matrix\n" +input +"\n\nWithTotals" +output;
    }
    public static String AddDiag()
    {  

        int totala = 0;
        int totalb = 0;
        int count = 5;
        for(int i=0; i<5; i++)
        {
            count--;
            totala += input[i][i];
            totalb += input[i][count];
        }

        return "Main Diagonal Sum " +totala +"\nOther Diagonal Sum" +totalb;
    }

}

I didnt have the arrays initialized...

public static int[][] input = new int[5][5];
public static int[][] output = new int[5][5];
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.