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 2 of 2

Thread: Why is my Java Programme getting Problems ?? (Exception in thread "main" )

  1. #1
    Junior Member
    Join Date
    May 2013
    Posts
    12
    Thanks
    0
    Thanked 6 Times in 2 Posts

    Default Why is my Java Programme getting Problems ?? (Exception in thread "main" )

    HI

    i have been working on this project for a pair of days and i dont remember if i had this problem at the start as well or not ...

    because the programme sometime works perfectly and sometime Gives me this error :

    Exception in thread "main" java.lang.NullPointerException
    at java.awt.Container.addImpl(Container.java:1090)
    at java.awt.Container.add(Container.java:410)
    at d.dproject.DPuzzle.<init>(DPuzzle.java:61)
    at d.dproject.DDProject.main(DDProject.java:46)


    anyway .. can you please help me to figure out the problem .. i am a beginner in java .. and need much progress

    there are 3 classes ..

    Main one is a GUI which calls other classes ...
    one is a type of DnD of text ..
    and last one which i was creating when i got the error is a puzzle in developing ..

    package d.dproject;
    import javax.swing.*;
    import java.awt.*;;
    import java.io.*;
    import java.awt.event.*;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
     
     
     
     
    public class DDProject implements ActionListener{
        JFrame DDFrame;
        JRadioButton puzzle,textDrag;
        DPuzzle a;
        TxTDrag b;
        //Costruttore
        DDProject (){
            a = new DPuzzle();
            b = new TxTDrag();
            DDFrame = new JFrame("Drag and Drop");
            DDFrame.setDefaultCloseOperation(DDFrame.EXIT_ON_CLOSE);
            DDFrame.setBounds(100,100,200,150);
            DDFrame.setLayout(new GridLayout(2,1));
     
     
            ButtonGroup radButtons = new ButtonGroup();
            puzzle = new JRadioButton("Puzzle");
            puzzle.addActionListener(this);
            textDrag = new JRadioButton("DnD Contenuto Files");
            textDrag.addActionListener(this);
            radButtons.add(puzzle);
            radButtons.add(textDrag);
     
     
            DDFrame.add(textDrag);
            DDFrame.add(puzzle);
            DDFrame.setVisible(true);
        }
     
     
        public static void main(String[] args) {
            new DDProject();
            new TxTDrag();
            new DPuzzle();
        }
     
        @Override
        public void actionPerformed(ActionEvent ae) {
            if (ae.getSource()==puzzle){
                a.DPFrame.setVisible(true);
            }
            if (ae.getSource()==textDrag){
                b.txtDragf.setVisible(true);
            }
        }
    }

    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package d.dproject;
     
    import javax.swing.*;
    import java.awt.*;;
    import java.io.*;
    import java.awt.event.*;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
     
     
     
     
    /**
     *
     * @author Haider Ali
     */
    public class TxTDrag implements ActionListener{
        JTextArea txtArea1,txtArea2;            //file 1 e 2
        JTextField nameFile1,nameFile2;         //Nomi dei File     
        JFrame txtDragf;                               //Frame Principale
        JButton show1,show2,writeF;                           //Bottoni file1 e file2
        JCheckBox ckBx;                                 //CheckBox
     
        public TxTDrag(){
            txtDragf = new JFrame("DnD Testo");
            //f.setLayout(new BorderLayout());
            txtDragf.setBounds(100,100,700,500);
     
     
            JPanel centerPane = new JPanel();
            centerPane.setLayout(new GridLayout(2,2,10,10));
            JPanel northPane = new JPanel();
            northPane.setLayout(new GridLayout(3,2,10,10));
     
     
            nameFile1 = new JTextField();
            nameFile2 = new JTextField();
     
            txtArea1 = new JTextArea();
            txtArea2 = new JTextArea();
            JScrollPane scr1 = new JScrollPane(txtArea1);
            JScrollPane scr2 = new JScrollPane(txtArea2);
     
            show1 = new JButton("Visualizza");
            show1.addActionListener(this);
            show2 = new JButton("Visualizza");
            show2.addActionListener(this);
            writeF = new JButton("Save");
            writeF.addActionListener(this);
     
            //CheckBox per Drag and Drop
            ckBx = new JCheckBox ("Attiva Drag & Drop del Testo");
            ckBx.addActionListener(this);
     
            northPane.add(new JLabel("Nome File1 :"));
            northPane.add(new JLabel("Nome File2 :"));
            northPane.add(nameFile1);
            northPane.add(nameFile2);
            northPane.add(new JLabel("TextArea per File1 :"));
            northPane.add(new JLabel("TextArea per File2 :"));
            centerPane.add(scr1);
            centerPane.add(scr2);
            centerPane.add(show1);
            centerPane.add(show2);
     
            JPanel southPane = new JPanel ();
            southPane.add(ckBx);
            southPane.add(writeF);
     
     
            txtDragf.add(northPane,BorderLayout.NORTH);
            txtDragf.add(centerPane,BorderLayout.CENTER);
            txtDragf.add(southPane,BorderLayout.SOUTH);
            txtDragf.setVisible(false);
        }
     
        @Override
        public void actionPerformed(ActionEvent ae) {
            //Bottone per File 1
     
            if (ae.getSource()==show1){
                String fileNome = nameFile1.getText();
                String par="";
                FileReader fr = null;
                try {
                    fr = new FileReader(fileNome);
                    BufferedReader br = new BufferedReader(fr);
                    try {
                        par = br.readLine();
                        txtArea1.setText("");
     
                        while(par!=null){
                            txtArea1.append(par+"\n");
                            par = br.readLine();
                        }
                        br.close();
                    } catch (IOException ex) {
                        System.out.println("Error");
                    }
                } catch (FileNotFoundException ex) {
                    JOptionPane.showMessageDialog(null, "Il Nome per File 1 inserito non Esiste", "Error Esistenza",JOptionPane.ERROR_MESSAGE);
                }
            }
     
            //Bottone per File 2
     
            if (ae.getSource()==show2){
                String fileNome = nameFile2.getText();
                String par="";
                FileReader fr = null;
                try {
                    fr = new FileReader(fileNome);
                    BufferedReader br = new BufferedReader(fr);
                    try {
                        par = br.readLine();
                        txtArea2.setText("");
     
                        while(par!=null){
                            txtArea2.append(par+"\n");
                            par = br.readLine();
                        }
                        br.close();
                    } catch (IOException ex) {
                        System.out.println("Error");
                    }
     
     
                } catch (FileNotFoundException ex) {
                    JOptionPane.showMessageDialog(null, "Il Nome per File 2 inserito non Esiste", "Error Esistenza",JOptionPane.ERROR_MESSAGE);
                }
            }
            //fine Source button
     
     
            //Inizio CheckBox
            if (ae.getSource()==ckBx){
                if (ckBx.isSelected()){             //se checkbox è selezionato
                    txtArea1.setDragEnabled(true);  //il contenuto di txtArea sara dragAble
                    txtArea2.setDragEnabled(true);  
                }else{
                    txtArea1.setDragEnabled(false);
                    txtArea2.setDragEnabled(false);
                }
            }
            //Fine Source checkBox
     
     
     
     
            //Inizio Scrittura
            if (ae.getSource()==writeF){
     
                String fileNome1 = nameFile1.getText();
                String par1="";
                String fileNome2 = nameFile2.getText();
                String par2="";
     
                PrintWriter outputStream1 = null;
                try {
                    outputStream1 = new PrintWriter(new FileOutputStream(fileNome1));
                    par1 = txtArea1.getText();
                    outputStream1.println(par1);
                    outputStream1.close();
                } catch (FileNotFoundException ex) {
                    System.out.println("Error "+fileNome1);
                    System.exit(0);
                }
     
                PrintWriter outputStream2 = null;
                try {
                    outputStream2 = new PrintWriter(new FileOutputStream(fileNome2));
                    par2 = txtArea2.getText();
                    outputStream2.println(par2);
     
                    outputStream2.close();
                } catch (FileNotFoundException ex) {
                    System.out.println("Error "+fileNome2);
                    System.exit(0);
                }
     
     
            } 
            }
        }

    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package d.dproject;
    import javax.swing.*;
    import java.awt.*;;
    import java.io.*;
    import java.awt.event.*;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import java.util.*;
     
    public class DPuzzle implements ActionListener{
        JFrame DPFrame;
        ImageIcon []img;
     
        DPuzzle(){
            DPFrame = new JFrame("PUZZLE");
            //DPFrame.setDefaultCloseOperation(DPFrame.EXIT_ON_CLOSE);
            DPFrame.setBounds(200,100,700,500);
            DPFrame.setLayout(new BorderLayout());
     
     
            JPanel westPane = new JPanel();
            westPane.setLayout(new GridLayout(3,3,7,7));
            JPanel eastPane = new JPanel();
            eastPane.setLayout(new GridLayout(3,3,7,7));
     
            img = new ImageIcon[9];
            img[0] = new ImageIcon("1.jpg");
            img[1] = new ImageIcon("2.jpg");
            img[2] = new ImageIcon("3.jpg");
            img[3] = new ImageIcon("4.jpg");
            img[4] = new ImageIcon("5.jpg");
            img[5] = new ImageIcon("6.jpg");
            img[6] = new ImageIcon("7.jpg");
            img[7] = new ImageIcon("8.jpg");
            img[8] = new ImageIcon("9.jpg"); 
     
            JLabel []lbl = new JLabel[9];
     
            int []vet = new int[9];
            int n;    
            boolean trovato= false;
     
            for (int i=0;i<9;i++){
                do{
                    trovato=false;
                    n=(int)(Math.random()*9);
                    for (int j=0;j<5;j++){
                        if (n==vet[j])
                            trovato=true;
                    }
                }while(trovato);
                vet[i]=n;
                lbl[n]= new JLabel(img[i]);
            }
     
            for (int i=0;i<9;i++) {
                westPane.add(lbl[i]);
            }
            //Fine Pannello Ovest
     
     
     
     
     
     
     
     
            DPFrame.add(westPane,BorderLayout.WEST);
            DPFrame.add(eastPane,BorderLayout.EAST);
            DPFrame.setVisible(false);
        }
     
        @Override
        public void actionPerformed(ActionEvent ae) {
     
        }
     
    }
    [ATTACH]2064[/ATTACH]


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Why is my Java Programme getting Problems ?? (Exception in thread "main" )

    Exception in thread "main" java.lang.NullPointerException
    at java.awt.Container.addImpl(Container.java:1090)
    at java.awt.Container.add(Container.java:410)
    at d.dproject.DPuzzle.<init>(DPuzzle.java:61)
    There is a variable with a null value on line 61. Look at line 61 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 61 and print out the values of all the variables on that line.

    The code in the main() method looks unusual. It creates three class objects and doesn't use the reference returned by the new statements anywhere.

    Also variable names don't follow coding standards of starting with a lowercase letter. That makes the code confusing to read because the naming convention says that class names start with uppercase letters.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 1
    Last Post: April 7th, 2013, 03:40 PM
  2. Replies: 2
    Last Post: August 30th, 2012, 09:45 AM
  3. Replies: 3
    Last Post: December 7th, 2011, 02:03 AM
  4. Replies: 6
    Last Post: November 12th, 2010, 04:40 AM
  5. Replies: 2
    Last Post: March 26th, 2010, 11:22 AM