Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Help WordSearchProject

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help WordSearchProject

    I keep getting the nullPointerException for this. It's a word search program. I'm aware that its one of the ugliest programs you will ever see, but I just need it to work. Every class name is underlined in yellow and the entire 2d Object array is underlined in yellow. Heres the error message in console:
    Exception in thread "main" java.lang.NullPointerException
    at ProjGui.<init>(ProjGui.java:49)
    at WordsForSearch.<init>(WordsForSearch.java:18)
    at ProjGui.main(ProjGui.java:91)

    and heres the program. It has two classes

    [import javax.swing.*;
    public class ProjGui extends JFrame {
    String std;
    String[] ten1;
    String[] ten2;
    String[] nine1;
    String[] eight1;
    String[] eight2;
    String[] seven1;
    String[] six1;
    String[] five1;
    String[] four1;
    String[] four2;
    String[] three1;
    String[] three2;
    String[] three3;
    private JTable table;
    //static Scanner inputStream = null;
    //static Scanner kb = new Scanner(System.in);
    //int width;
    //int length;
    public ProjGui() {
    getContentPane().setLayout(new GridLayout(1, 0, 0, 0));

    table = new JTable();
    table.setToolTipText("");
    table.setColumnSelectionAllowed(true);
    table.setCellSelectionEnabled(true);
    table.setBorder(new BevelBorder(BevelBorder.LOWERED, SystemColor.controlHighlight, SystemColor.textHighlight, null, null));
    table.setBackground(Color.WHITE);
    table.setSelectionMode(ListSelectionModel.MULTIPLE _INTERVAL_SELECTION);
    table.setModel(new DefaultTableModel(
    new Object[][] {
    {"g", "a", "b", ten2[9], ten2[8], ten2[7], ten2[6], ten2[5], ten2[4], ten2[3], ten2[2], ten2[1], ten2[0], "m", "n", "z", "a", "q", "b", "e"},
    {"m", "f", "a", "j", "b", "v", "e", "j", "a", "s", "d", "h", "j", "s", four1[3], "a", "i", "n", "b", eight2[0]},
    {"g", "y", eight2[7], "m", "l", "p", "o", "q", "e", "u", "z", "a", "o", "p", "e", four1[2], "c", "b", "e", eight2[1]},
    {"l", "x", "e", eight2[6], "b", "n", "o", "d", "l", "p", "d", "m", "a", "p", "o", "l", four1[1], "g", "a", eight2[2]},
    {ten1[0], "l", "h", "r", eight2[5], "t", "l", "l", "u", "l", "a", "m", "h", "c", "v", "a", "e", four1[0], "w", eight2[3]},
    {ten1[1], "a", "s", "a", "g", eight2[4], "j", "u", "e", "s", "l", "e", "l", "t", "p", "o", "b", "s", "g", eight2[4]},
    {ten1[2], "m", "n", "w", "u", "l", eight2[3], "r", "y", "g", "w", "s", "w", "a", "t", "d", "k", "t", "s", eight2[5]},
    {ten1[3], "e", "a", "r", "r", "t", "m", eight2[2], "e", "o", "a", "k", "o", "c", "a", "l", "u", "n", "j", eight2[6]},
    {ten1[4], "a", "m", "n", "e", "e", "e", "v", eight2[1], "o", "a", "i", "b", "o", "e", "a", "g", seven1[6], "d", eight2[7]},
    {ten1[5], "v", "u", "j", "n", "c", "i", "u", "x", eight2[0], "g", "m", "a", "m", "m", "s", seven1[5], "u", "e", "l"},
    {ten1[6], "c", four2[0], four2[1], four2[2], four2[3], "c", "o", "o", "b", eight2[6], "o", "v", "a", "a", seven1[4], "w", five1[5], "n", "a"},
    {ten1[7], "u", "j", "a", "x", "l", "a", "e", "l", "y", "i", "b", "e", "c", seven1[3], "s", "x", five1[4], "v", "c"},
    {ten1[8], "e", "h", "t", "z", "a", "r", "f", "p", "p", "n", "r", "e", seven1[2], "y", "o", "p", five1[3], "e", "y"},
    {ten1[9], "k", "n", "h", "o", "n", three3[2], "o", "r", "o", "n", "o", seven1[1], "a", "o", "g", "v", five1[2], "u", "v"},
    {"z", "j", "i", "a", "p", "o", three3[1], "u", "e", "w", "o", seven1[0], "h", "r", "g", "s", "h", six1[1], "y", "e"},
    {"n", three1[0], three1[1], three1[2], "q", "w", three3[0], "r", "g", "l", "s", "t", "s", "t", "o", "e", "r", six1[0], "q", five1[4]},
    {"p", "a", "m", "w", "k", "e", "b", "e", "n", "m", "r", "h", "e", "h", "m", "m", "i", "y", five1[3], "b"},
    {"s", "u", "r", "j", "h", "t", "a", "v", "a", "e", "e", "e", three2[2], three2[1], three2[0], "j", "u", five1[2], "m", "v"},
    {"f", "e", "e", "s", "g", "s", "h", "a", "f", "h", "p", "r", "h", "u", "o", "k", five1[1], "g", "d", "n"},
    {"h", "z", eight1[7], eight1[6], eight1[5], eight1[4], eight1[3], eight1[2], eight1[1], eight1[0], "a", "s", "t", "r", "d", five1[0], "g", "a", "l", "j"},
    },
    new String[] {
    "", "New column", "New column", "New column", "New column", "New column", "New column", "New column", "New column", "New column", "New column", "New column", "New column", "New column", "New column", "New column", "New column", "New column", "New column", "New column"
    }
    ) {
    Class[] columnTypes = new Class[] {
    String.class, Object.class, Object.class, Object.class, Object.class, Object.class, Object.class, Object.class, Object.class, Object.class, Object.class, Object.class, Object.class, Object.class, Object.class, Object.class, Object.class, Object.class, Object.class, Object.class
    };
    public Class getColumnClass(int columnIndex) {
    return columnTypes[columnIndex];
    }
    });
    table.setForeground(Color.BLACK);
    int column;
    int row;
    //table.getValueAt(row, column);
    getContentPane().add(table);

    }

    public static void main(String[] args) {

    WordsForSearch obj1 = new WordsForSearch();
    obj1.getWords();
    obj1.writeToFile();
    ProjGui gui = new ProjGui();
    gui.setVisible(true);


    }
    }
    ]


    //That's the main class
    //This is the child class that has all my objects...called WordsForSearch
    [import javax.swing.*;
    import java.util.*;
    import java.awt.GridLayout;
    import java.awt.Color;
    import javax.swing.table.DefaultTableModel;
    import javax.swing.table.TableCellRenderer;
    import java.awt.SystemColor;
    import java.io.BufferedWriter;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileWriter;
    import java.util.Scanner;

    import javax.swing.border.BevelBorder;
    public class WordsForSearch extends ProjGui {
    String first10;String sec10;String nineStr;String eightFi;
    String eightSec; String sevenFi; String sixFi;String fiveFi;
    String fourFi; String fourSec;String threeFi; String threeSec;
    String threeTh;
    public void getWords() {

    first10 = JOptionPane.showInputDialog("What is the first 10 letter word");
    first10= first10.replace("", " ").trim();
    ten1 = first10.split(" ");

    sec10 = JOptionPane.showInputDialog("What is the second 10 letter word?");
    sec10= sec10.replace("", " ").trim();
    ten2 = sec10.split(" ");

    nineStr = JOptionPane.showInputDialog("What is the 9 letter word?");
    nineStr= nineStr.replace("", " ").trim();
    nine1 = nineStr.split(" ");

    eightFi = JOptionPane.showInputDialog("What is the first 8 letter word?");
    eightFi = eightFi.replace("", " ").trim();
    eight1 = eightFi.split(" ");

    eightSec = JOptionPane.showInputDialog("What is the second 8 letter word?");
    eightSec = eightSec.replace("", " ").trim();
    eight2 = eightSec.split(" ");

    sevenFi = JOptionPane.showInputDialog("What is the 7 letter word?");
    sevenFi = sevenFi.replace("", " ").trim();
    seven1 = sevenFi.split(" ");

    sixFi = JOptionPane.showInputDialog("What is the 6 letter word?");
    sixFi = sixFi.replace("", " ").trim();
    six1 = sixFi.split(" ");

    fiveFi = JOptionPane.showInputDialog("What is the 5 letter word?");
    fiveFi = fiveFi.replace("", " ").trim();
    five1 = fiveFi.split(" ");

    fourFi = JOptionPane.showInputDialog("What is the first 4 letter word?");
    fourFi = fourFi.replace("", " ").trim();
    four1 = fourFi.split(" ");

    fourSec = JOptionPane.showInputDialog("What is the second 4 letter word?");
    fourSec = fourSec.replace("", " ").trim();
    four2 = fourSec.split(" ");

    threeFi = JOptionPane.showInputDialog("What is the first 3 letter word?");
    threeFi = threeFi.replace("", " ").trim();
    three1 = threeFi.split(" ");

    threeSec = JOptionPane.showInputDialog("What is the second 3 letter word?");
    threeSec = threeSec.replace("", " ").trim();
    three2 = threeSec.split(" ");

    threeTh = JOptionPane.showInputDialog("What is the third 3 letter word?");
    threeTh = threeTh.replace("", " ").trim();
    three3 = threeTh.split(" ");
    }
    public void writeToFile() {
    FileWriter fWriter = null;
    BufferedWriter writer = null;
    try {
    fWriter = new FileWriter("IAFile.txt");
    writer = new BufferedWriter(fWriter);
    writer.write(first10);
    writer.newLine();
    writer.write(sec10);
    writer.newLine();
    writer.write(nineStr);
    writer.newLine();
    writer.write(eightFi);
    writer.newLine();
    writer.write(eightSec);
    writer.newLine();
    writer.write(sevenFi);
    writer.newLine();
    writer.write(sixFi);
    writer.newLine();
    writer.write(fiveFi);
    writer.newLine();
    writer.write(fourFi);
    writer.newLine();
    writer.write(fourSec);
    writer.newLine();
    writer.write(threeFi);
    writer.newLine();
    writer.write(threeSec);
    writer.newLine();
    writer.write(threeTh);
    writer.close();
    System.out.println("Your input was saved");
    } catch (Exception e) {
    System.out.println("File not Found please fix errors!");
    }
    }


    }
    ]

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Help WordSearchProject

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.

    Exception in thread "main" java.lang.NullPointerException
    at ProjGui.<init>(ProjGui.java:49)
    at WordsForSearch.<init>(WordsForSearch.java:18)
    at ProjGui.main(ProjGui.java:91)
    There is a variable with a null value on line 49. Look at line 49 in the your source and see what variable is null. Then backtrack in the code to see why that variable does not have a valid value.
    If you can not tell which variable it is, add a println just before line 49 and print out the values of all the variables on that line.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jan 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help WordSearchProject

    that's the thing it shouldn't be null

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Help WordSearchProject

    Of course. But computers are pretty stupid. They do exactly what you tell them to do, not what you want them to do.

    Did you find what variable was null?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jan 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help WordSearchProject

    Well I'm fairly sure it has to do with the Object[][] not being declared earlier, but I don't know how considering windowbuilder gave it no name and it is exclusively for the jtable in the gui.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Help WordSearchProject

    Does it have a non-null value? What is printed if you print it?

    What code is on line 49?

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

Tags for this Thread