Java: basic gui issue

Coding Critique is the place to post source code for peer review by other members of DevNetwork. Any kind of code can be posted. Code posted does not have to be limited to PHP. All members are invited to contribute constructive criticism with the goal of improving the code. Posted code should include some background information about it and what areas you specifically would like help with.

Popular code excerpts may be moved to "Code Snippets" by the moderators.

Moderator: General Moderators

Post Reply
afbase
Forum Contributor
Posts: 113
Joined: Tue Aug 15, 2006 1:29 pm
Location: SoCAL!!!!

Java: basic gui issue

Post by afbase »

In my IDE, eclipse, I get this error from running my code. Only problem is, I don't exactly why this is occurring. I'm still new to coding in Java, and I'm still learning its finicky ways. Could somebody point me in the right direction in my code?
Exception in thread "main" java.lang.NullPointerException
at java.awt.Container.addImpl(Unknown Source)
at java.awt.Container.add(Unknown Source)
at sudoku.buildSudoku_pane(sudoku.java:25)
at sudoku.<init>(sudoku.java:31)
at sudoku.main(sudoku.java:46)

Code: Select all

 
import javax.swing.*;
 
import java.awt.*;
import java.awt.event.*;
import java.lang.*;
import java.io.*;
import java.util.*;
import java.lang.Character;
 
 
public class sudoku extends JFrame implements ActionListener{
private JFrame sudoku_frame = new JFrame("Sudoku");
private JButton solve;
private JButton check_if_correct;
private JTextField[] sudoku_entry;
public final static boolean RIGHT_TO_LEFT = false;
public void actionPerformed(ActionEvent e) {}
private void buildSudoku_pane(int number, Container pane, JTextField[] sudoku_entry_array){
    sudoku_entry_array = new JTextField[number*number];
    if (RIGHT_TO_LEFT){
    pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
    }
    pane.setLayout(new GridLayout(number,number));
    for(int i = 0; i <= (number*number)-1; i++){
        pane.add(sudoku_entry_array[i]);
    }
}
 
public sudoku() {
    int int_default = 9;
    buildSudoku_pane(int_default, sudoku_frame.getContentPane(), sudoku_entry);
    sudoku_frame.pack();
    sudoku_frame.setVisible(true);
}   
 
public sudoku(int number){
    buildSudoku_pane(number, sudoku_frame.getContentPane(), sudoku_entry);
    sudoku_frame.pack();
    sudoku_frame.setVisible(true);
}
 
 
 
public static void main(String[] args) {
 
    sudoku tpo = new sudoku();               // create 'the program object'
 
    tpo.addWindowListener(new WindowAdapter() {   // this exits the program when X box clicked
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    });
 
} // end of main
 
}
 
 
 
User avatar
emmbec
Forum Contributor
Posts: 112
Joined: Thu Sep 21, 2006 12:19 pm
Location: Queretaro, Mexico

Re: Java: basic gui issue

Post by emmbec »

In your function buildSudoku_pane() the array sudoku_entry_array is not initialized, it has a length of 81 but filled with NULL objects, so when you are looping, you are accessing its contents and adding them to your pane, and since they are NULL it crashes, that is why it is saying NULL POINTER EXCEPTION 8) look:

Code: Select all

for (int i = 0; i <= (number * number) - 1; i++) {
            pane.add([b]sudoku_entry_array[i][/b]);  [color=#00BF00] //The array contents are NULL therefore Java throws a NULL POINTER EXCEPTION[/color]
        }
Debug your code and you will see.
Post Reply