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]