I am trying to create a 2d table that asks for user input to create table size, adds all the numbers across each row, and creates the next row by multiplying by the first row. Here's what I have and it's now giving me an "Exception in thread "main" java.lang.NullPointerException" error.

Any help is greatly appreciated!

public static void main(String[] args) {
Scanner input = new Scanner (System.in);

    int[][] table = new int [11][];
    int[] row = new int[0];

    System.out.println("Enter integer to create table size: ");
    int numRows = input.nextInt();

    for (int i=0; i<table.length; i++){
        for (int j=0; j<table[i].length; j++) {
            row = rowTotals(row);
    table[i] = new int [i*2];
    System.out.print(table[row][j] + " ");
                      }
     }
    }
    public static int[] rowTotals (int[] prevRow) {
    int num = 1;
    int[] row = new int[prevRow.length];
    row[0] = 1;

    for (int i=1; i<row.length; i++) {
    row[i] = prevRow[i];
    num++;
     }
    return row;
         }
    }

For which line in your code are you getting null pointer exception??
FYI you get null pointer exception whenever you try to access/use something whose value is null

row[0] = 1;

I guess this is where the problem is. Your array length is actually 0 and you are trying to access the first element..!!!

Ok, here's what I changed it to and the error is:
Exception in thread "main" java.lang.NullPointerException
at extra2.main(extra2.java:30)

I'm not sure what it's wanting me to change!

import java.util.Scanner;

public class extra2 {

public static void main(String[] args) {

Scanner input = new Scanner (System.in);


int[][] table = new int [11][];

int[] row = new int[1];


System.out.println("Enter integer to create table size: ");

int numRows = input.nextInt();

for (int i=0; i<table.length; i++){

for (int j=1; j<table[i].length; j++) {

row = rowTotals(row);

table[i] = new int [i*2];

System.out.print(table[i][j] + " ");
        }

    }

}

public static int[] rowTotals (int[] prevRow) {

int num = 1;

int[] row = new int[prevRow.length];


for (int i=1; i<row.length; i++) {

row[i] = prevRow[i];

num++;

    }

return row;

 }

}

So which line was line 30 in your source code? - the way you posted it has screwed up the line numbers (and the lack of indentation makes it hard to read)
I don't know what rowTitles is supposed to do, but simply copying the input array execpt for the first element seems a curious thing to want to do ;)

Yikes, I'm sorry I didn't think about the numbers not lining up when I copied it. I'll have to check it again when I get home tonight. I appreciate the help. I know this code looks really screwed up. I am barely grasping this intro Java class with the way the teacher is teaching it. I'm trying to pull from previous assignments to put this one together, and it's becoming a matter of trial and error, mostly errors :S

Where do you use the value read in from the user into: numRows?

Apparently it's not reading in anywhere. It's supposed to set the table size according to user input, but I feel I'm missing something.

A value is read from the user at line 11 but it is not used for anything.
The question to the user was to enter the table size. The code should use the value entered to create the table.

I've been working on this for a couple days now, and I guess I'm at a loss on how to get the input to create the table size because when I try to, it says error this is a variable.

Please post the full text of the error message and the code that causes it.

Line 11 reads in the size from the user.

int numRows = input.nextInt();

Ok I've got it to run, but this is what it outputs:
Enter integer to create table size:
2
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

Here's how I've edited it so far:

import java.util.Scanner;

public class extra2 {
public static void main(String[] args) {
Scanner input = new Scanner (System.in);

    int[][] table = new int [11][10];
    int[] row = new int[1];
    int sum = 0;

    System.out.println("Enter integer to create table size: ");
    int numRows = input.nextInt();

    for (int i=0; i<table.length; i++){
        for (int j=0; j<table[i].length; j++) {
            row = rowTotals(row);
            table[0][0] = 1;
            sum += table[i][j];
            System.out.print(" " + table[i][j]);

            }
        }
    }
    public static int[] rowTotals (int[] prevRow) {
    int num = 1;
    int[] row = new int[prevRow.length];

        for (int i=1; i<row.length; i++) {
        row[i] = prevRow[i] + row[i];
        num++;
          }
        return row;
         }
    }

What do you do with the value in numRows? You ask the user a question and get his response and ignore it. Surely it should be used for something.

What is the program supposed to do? You've written 25+ lines of code without any plan for what the code is to do.
For example what is the purpose of the loop from lines 8 to 17? Why is it there?
Can you list the steps you want the program to do to solve your problem?

Sure. The program is supposed to ask the user how big to make the table. Then create a table that's 11 numbers across, adding all the numbers into a total in the 11th spot. Then each row is a multiplication of the first row.
Here is an example of an input of 10.

1 2 3 4 5 6 7 8 9 10 55
2 4 6 8 10 12 14 16 18 20 1100
...
10 20 30 40 50 60 70 80 90 100 550

Lines 8-17 were supposed to create the table. I'm sure it's way off. I know this code looks atrocious. I appreciate the help!

The program is supposed to ask the user how big to make the table. Then create a table that's 11 numbers across,

Why ask for a number if you are going to create a table with 11 rows. What is the purpose of asking if you ignore the answer?

Here is an example of an input of 10.

1 2 3 4 5 6 7 8 9 10 55

What is the 11th number: 55?

I was trying to set the columns at 11 and trying to get the user input to set how many rows there would be.
So there would be 11 columns, with the total of all the numbers in the 11th column.

The 55 (in the 11th column) is the adding of the numbers 1-10.
The 1100 is the adding of the numbers 2-20 in the 2nd row
etc...

Is this not possible to do that way? Do I need to have the user input rows and columns instead of setting the columns and having the user set the rows?

You still have not explained why you ask the user for a number that the program does not use.

The problem was a NPE. Have you solved that problem yet?

Yes, it's no longer giving me an NPE, but it's still not outputting correctly. I guess I'm at a loss here on how to get the program to use the user input. I tried taking pieces from a Pascal Triangle code we used in the past to create this program, but it's obviously not working. Everytime I have tried to incorporate the user input somewhere, it gives me an error when compiling that it's a variable and it won't take it.

The main problem I see with the program is that there does not appear to be a design for the code that has been written. I can not see any logic in what the code does.

Before writing any more code, you need to make a list of the steps that the code needs to do to solve the problem. Post the list of steps the program should do in pseudo code and get the logic worked out for the program before writing any more code. Make each step be as simple as possible, something that is very close to what a line of code would do. Untll you get good logic, writing more code is a waste of time.

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.