Results 1 to 6 of 6
- 12-21-2011, 04:01 AM #1
Member
- Join Date
- Dec 2011
- Posts
- 4
- Rep Power
- 0
Got struck with this :- " Exception in thread "main" java.lang.NullPointerException"
Java Code:package radhika; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; import java.awt.Font; import java.awt.Image; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.DataInputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.net.Socket; import java.net.UnknownHostException; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JScrollPane; import javax.swing.JTextArea; public class NodeA implements ActionListener { public Font f = new Font("Algerian" , Font.BOLD + Font.ITALIC, 45); public Font f1 = new Font("Calibrie", Font.BOLD + Font.ITALIC, 25); public Font f2 = new Font("Calibrie", Font.BOLD + Font.ITALIC, 15); public Font f5 = new Font("Bell MT", Font.BOLD, 20); public Font f6 = new Font("Bell MT", Font.BOLD, 18); public Font f3 = new Font("Bell MT", Font.BOLD , 25); public Font f4 = new Font("Arial Rounded MT Bold", Font.BOLD , 35); public JLabel T3= new JLabel("Mitigation of Control Channel Jamming Under"); public JLabel T4= new JLabel("Node Capture Attacks"); public JLabel ieee = new JLabel("IEEE Transaction On Mobile computing"); public JLabel vol = new JLabel("(VOL. 8, No. 6, September 2009)"); public JLabel T1= new JLabel("NODE A"); public JLabel T2= new JLabel("Select the File"); public JComboBox TAreu = new JComboBox(); public JButton btn = new JButton("Submit"); public JTextArea tf = new JTextArea(); public JScrollPane pane = new JScrollPane(); public JButton btn1 = new JButton("Browse"); public JFrame jf; public Container c; NodeA() { JLabel imageLabel = new JLabel(); ImageIcon ii = new ImageIcon(this.getClass().getResource( "channal jamming.gif")); imageLabel.setIcon(ii); imageLabel.setBounds(20,220,500,340); imageLabel.setBackground(new Color(193,222,216)); jf = new JFrame("NODE A"); c = jf.getContentPane(); c.setLayout(null); jf.setSize(1030, 750); c.setBackground(new Color(193,222,216)); T2.setBounds(560, 230, 250,45); T2.setFont(f3); T2.setForeground(new Color(120,120,0)); T2.setForeground(Color.BLUE); btn1.setBounds(780,230,230,45); btn1.setFont(f3); btn1.setForeground(Color.BLACK); T1.setBounds(450,150,250,45); T1.setForeground(Color.RED); TAreu.setBounds(550,550,200,35); btn.setBounds(550,620,230,45); T1.setFont(f4); btn.setFont(f3); btn.setForeground(Color.BLACK); TAreu.addItem("Mesh Route"); TAreu.addItem(" Node "); TAreu.setFont(f3); TAreu.setBackground(Color.WHITE); TAreu.setForeground(Color.BLUE); pane.setBounds(550,300,400,200); tf.setColumns(20); tf.setRows(10); tf.setName("tf"); pane.setName("pane"); pane.setViewportView(tf); btn1.setBackground(new Color(176,174,144)); btn.setBackground(new Color(176,174,144)); btn1.addActionListener(this); btn.addActionListener(this); T3.setBounds(140, 3, 950, 50); T3.setForeground(new Color(10, 120, 7)); T3.setFont(f4); T4.setBounds(320, 40, 950, 50); T4.setForeground(new Color(10, 120, 7)); T4.setFont(f4); ieee.setBounds(350, 70, 950, 50); ieee.setForeground(Color.BLACK); ieee.setFont(f5); vol.setBounds(390, 100, 950, 50); vol.setForeground(Color.BLACK); vol.setFont(f6); btn1.setMnemonic(KeyEvent.VK_B); btn.setMnemonic(KeyEvent.VK_S); jf.show(); c.add(T1); c.add(btn); c.add(T2); c.add(pane, BorderLayout.CENTER); c.add(btn1); c.add(imageLabel); c.add(T3); c.add(T4); c.add(ieee); c.add(vol); jf.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent win) { System.exit(0); } }); } public void actionPerformed(ActionEvent e) { DataInputStream input; BufferedInputStream bis; BufferedOutputStream bos = null; BufferedWriter writer = null; int in; byte[] byteArray; String strLine = null; String newline = "\n"; if (e.getSource()== btn1) { JFileChooser chooser = new JFileChooser(); // Set the current directory to the application's current directory try { // Create a File object containing the canonical path of the // desired file File f = new File(new File("filename.txt").getCanonicalPath()); // Set the selected file chooser.setSelectedFile(f); } catch (IOException e1) { } // Show the dialog; wait until dialog is closed chooser.showOpenDialog(btn1); // Get the currently selected file File curFile = chooser.getSelectedFile(); try{ // Open the file that is the first // command line parameter FileInputStream fstream = new FileInputStream(curFile); // Get the object of DataInputStream DataInputStream ins = new DataInputStream(fstream); BufferedReader br = new BufferedReader(new InputStreamReader(ins)); StringBuffer buffer = new StringBuffer(); while ((strLine = br.readLine()) != null) { // Print the content on the console System.out.println (strLine); buffer.append(strLine+ "\n"); } tf.setText(buffer.toString()); //Close the input stream // ins.close(); // bis.close(); // bos.close(); }catch (Exception e1){//Catch exception if any System.err.println("Error: " + e1.getMessage()); } } if(e.getSource()== btn) { Socket client = null; try { client = new Socket("127.0.0.1", 8585); //input = new DataInputStream (client.getInputStream() ); //bis = new BufferedInputStream(new FileInputStream(curFile)); bos = new BufferedOutputStream(client.getOutputStream()); byteArray = tf.getText().getBytes(); bos.write(byteArray, 0, byteArray.length); bos.flush(); bos.close(); client.close(); } catch (UnknownHostException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } finally { if (bos != null) { try { bos.close(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } try { client.close(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } } } public static void main(String args[]) { new NodeA(); } }
Please help me out in this. Thanks in advance.Java Code:ERROR: Exception in thread "main" java.lang.NullPointerException at javax.swing.ImageIcon.<init>(ImageIcon.java:138) at radhika.NodeA.<init>(NodeA.java:73) at radhika.NodeA.main(NodeA.java:276) Java Result: 1Last edited by pbrockway2; 12-21-2011 at 04:04 AM. Reason: code tags added
- 12-21-2011, 04:19 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
Re: Got struck with this :- " Exception in thread "main" java.lang.NullPointerExcepti
Hi Vermont, welcome to the forum!
When you post code, use the "code" tags. You put [code] at the start of the code and [/code] at the end and this will preserve the formatting. It is also a good idea to spaces (consistently) rather than tabs to indent because of the rather generous (and unpredictable) way in which tabs are rendered on web pages.
Typically you read these stacktraces from the top down until you reach a line which refers to your code. The line it mentions is a good place to begin looking for problems. Line 73 says:Exception in thread "main" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(ImageIcon.java:138)
at radhika.NodeA.<init>(NodeA.java:73)
at radhika.NodeA.main(NodeA.java:276)
The exception was actually thrown by the ImageIcon constructor, so a good thing to check is whether image actually exists.Java Code:ImageIcon ii = new ImageIcon(this.getClass().getResource( "channal jamming.gif"));
[Edit] You should see a file:/ URL. If you are on Windows don't be put off by the forward slashes or the %20 representing a space. But do check that the file exists.Java Code:System.out.println("About to load image: " + this.getClass().getResource("channal jamming.gif")); ImageIcon ii = new ImageIcon(this.getClass().getResource( "channal jamming.gif"));Last edited by pbrockway2; 12-21-2011 at 04:22 AM.
- 12-21-2011, 04:21 AM #3
Re: Got struck with this :- " Exception in thread "main" java.lang.NullPointerExcepti
findInLine() returns null if the pattern isn't found and if this happens the call to charAt(0) will throw a NullPointerException.
To get around this problem split this line into two lines and test the value returned by findInLine() for null before calling charAt().
- 12-21-2011, 04:11 PM #4
Member
- Join Date
- Dec 2011
- Posts
- 4
- Rep Power
- 0
Re: Got struck with this :- " Exception in thread "main" java.lang.NullPointerExcepti
Hi pbrockway2,
Thanks a lot for the timely and speedy help..gif)
I will follow the code tags in my next posts, thank you so much.
Its working perfectly now.
The problem was with the "the ImageIcon constructor". I fixed it with your help..gif)
Great forum ... I love it... Cheers...
- 12-21-2011, 04:18 PM #5
Member
- Join Date
- Dec 2011
- Posts
- 4
- Rep Power
- 0
Re: Got struck with this :- " Exception in thread "main" java.lang.NullPointerExcepti
Hi SourCookie,
Thanks for ur timely response.
Got it and fixed with the help of this forum..
- 12-21-2011, 06:44 PM #6
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
Similar Threads
-
Exclamation Exception in thread "main" java.lang.NullPointerException
By Lorelai in forum New To JavaReplies: 5Last Post: 10-11-2011, 12:16 AM -
Exception in thread "main" java.lang.NullPointerException
By pavel in forum New To JavaReplies: 3Last Post: 08-22-2011, 08:31 AM -
Exception in thread "main" java.lang.NullPointerException
By syarizma in forum Advanced JavaReplies: 6Last Post: 08-06-2009, 11:50 AM -
Exception in thread "main" java.lang.NullPointerException
By farooqhussain786 in forum New To JavaReplies: 10Last Post: 04-03-2009, 06:31 AM -
ArrayList: Exception in thread "main" java.lang.NullPointerException
By susan in forum New To JavaReplies: 1Last Post: 07-16-2007, 06:32 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks