Null pointer Exception

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2009
Posts: 123
Reputation: neutralfox is an unknown quantity at this point 
Solved Threads: 0
neutralfox neutralfox is offline Offline
Junior Poster

Null pointer Exception

 
0
  #1
Apr 17th, 2009
Hello,
I am having a null pointer error, don't know where it came out. Can someone check this code for me please:

The error is in the sendBytes methods, I have put a comment over there, actually there are two null errors.

1.
  1. pw.println(filenm);


2.
  1. out.write(buffer,0,bytes);

Thanks a lot in advance.




This is the code:

  1. import java.net.*;
  2. import java.io.*;
  3. import javax.swing.JOptionPane;
  4.  
  5.  
  6. public class FTPClient implements Runnable {
  7.  
  8.  
  9. Socket s;
  10. long length;
  11. File file;
  12.  
  13. InputStreamReader in = null;
  14. OutputStream out = null;
  15. BufferedReader br = null;
  16. PrintWriter pw = null;
  17.  
  18. String filenm = null;
  19. String fn;
  20.  
  21. public FTPClient(){
  22.  
  23. }
  24.  
  25. public FTPClient(int port){
  26. connectToServer(port);
  27. new Thread( this ).start();
  28. }
  29.  
  30. //connect to the server
  31. public void connectToServer(int port){
  32.  
  33. try{
  34. s = new Socket ("127.0.0.1" , port);
  35. br = new BufferedReader(new InputStreamReader(s.getInputStream()));
  36. pw = new PrintWriter(s.getOutputStream(),true);
  37. out = s.getOutputStream();
  38.  
  39. JOptionPane.showMessageDialog(null, "Client says: ... I am connected and IO Ready!");
  40.  
  41. }catch(Exception e){e.printStackTrace();}
  42. }
  43.  
  44.  
  45.  
  46.  
  47. //send bytes to server
  48. public void sendBytes(File f){
  49.  
  50. try{
  51.  
  52. JOptionPane.showMessageDialog(null, "Client says: File send ....");
  53. JOptionPane.showMessageDialog(null, "Client says: The file name: " + f.getName());
  54. JOptionPane.showMessageDialog(null, "Client says: The file size: " + f.length());
  55.  
  56. filenm = f.getName();
  57. pw.println(filenm); // got a java.lang.NullPointerException here
  58. FileInputStream fis = new FileInputStream(filenm);
  59. byte[] buffer=new byte[1024];
  60. int bytes=0;
  61.  
  62. while((bytes=fis.read(buffer))!=-1)
  63. {
  64. out.write(buffer,0,bytes); // got a java.lang.NullPointerException here also when I comment // pw.println(filenm); the first one
  65. JOptionPane.showMessageDialog(null, "Test .... ");
  66. }
  67.  
  68. fis.close();
  69.  
  70. }catch(Exception e){e.printStackTrace();}
  71.  
  72. }
  73.  
  74.  
  75.  
  76. }


This is the error:

run-single:
java.lang.NullPointerException
at ignisftpv3.FTPClient.sendBytes(FTPClient.java:85)
at ignisftpv3.FTPClientInterface.btnSendActionPerformed(FTPClientInterface.java:121)
at ignisftpv3.FTPClientInterface.access$100(FTPClientInterface.java:8)
at ignisftpv3.FTPClientInterface$2.actionPerformed(FTPClientInterface.java:48)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6038)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3260)
at java.awt.Component.processEvent(Component.java:5803)
at java.awt.Container.processEvent(Container.java:2058)
at java.awt.Component.dispatchEventImpl(Component.java:4410)
at java.awt.Container.dispatchEventImpl(Container.java:2116)
at java.awt.Component.dispatchEvent(Component.java:4240)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)
at java.awt.Container.dispatchEventImpl(Container.java:2102)
at java.awt.Window.dispatchEventImpl(Window.java:2429)
at java.awt.Component.dispatchEvent(Component.java:4240)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)
BUILD SUCCESSFUL (total time: 3 minutes 10 seconds)
Last edited by neutralfox; Apr 17th, 2009 at 11:00 am.
Attached Files
File Type: java FTPClient.java (2.2 KB, 2 views)
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 1,026
Reputation: JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice 
Solved Threads: 151
JamesCherrill JamesCherrill is online now Online
Veteran Poster

Re: Null pointer Exception

 
0
  #2
Apr 17th, 2009
Is it possible that you have called the default constructor rather than the one with the port number, and thus connectToServer isn't executed, so the variables are not initialised when you call sendBytes?
ps you declare the class as implementing Runnable, but I see no run() method???
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 123
Reputation: neutralfox is an unknown quantity at this point 
Solved Threads: 0
neutralfox neutralfox is offline Offline
Junior Poster

Re: Null pointer Exception

 
0
  #3
Apr 17th, 2009
Yes ... the full code is in the attachement. I removed it from the code cause I am not dealing with this part yet.

The connectToServer is call as I saw the message in this method.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 1,026
Reputation: JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice 
Solved Threads: 151
JamesCherrill JamesCherrill is online now Online
Veteran Poster

Re: Null pointer Exception

 
0
  #4
Apr 17th, 2009
out.write(buffer,0,bytes); gives null pointer, so out, buffer, or bytes is not initialised. Find out which by printing them one at a time, then trace back to find out why the culprit wasn't initalised.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 123
Reputation: neutralfox is an unknown quantity at this point 
Solved Threads: 0
neutralfox neutralfox is offline Offline
Junior Poster

Re: Null pointer Exception

 
0
  #5
Apr 17th, 2009
Hello, thanks for the reply but:

They both have been initialized.

  1. byte[] buffer=new byte[1024];
  2. int bytes=0;
Last edited by neutralfox; Apr 17th, 2009 at 4:25 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 1,026
Reputation: JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice 
Solved Threads: 151
JamesCherrill JamesCherrill is online now Online
Veteran Poster

Re: Null pointer Exception

 
0
  #6
Apr 17th, 2009
Originally Posted by neutralfox View Post
They both have been initialized.
Yes, of course you think that, that's why it's a bug! Seriously, it's not working the way you expect, so you need to add diagnostic statements to find out what's happening. When debugging you just have to accept that your analysis of the code must be wrong. Anyway, it might be (as I said) the variable "out" as well.
Last edited by JamesCherrill; Apr 17th, 2009 at 5:03 pm.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 123
Reputation: neutralfox is an unknown quantity at this point 
Solved Threads: 0
neutralfox neutralfox is offline Offline
Junior Poster

Re: Null pointer Exception

 
0
  #7
Apr 17th, 2009
Okie , thanks for the fast reply. I will try it. Thanks a lot.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 21
Reputation: chili5 is an unknown quantity at this point 
Solved Threads: 2
chili5's Avatar
chili5 chili5 is offline Offline
Newbie Poster

Re: Null pointer Exception

 
0
  #8
Apr 17th, 2009
From a quick look:

  1. pw.println(filenm); // got a java.lang.NullPointerException here

You have never initialized a pw object. pw is set to null. Now you are initializing pw in the connectToServer method but perhaps that method isn't called, or it is called and an exception was thrown so the variable wasn't initialized?
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 1,026
Reputation: JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice 
Solved Threads: 151
JamesCherrill JamesCherrill is online now Online
Veteran Poster

Re: Null pointer Exception

 
0
  #9
Apr 18th, 2009
Originally Posted by chili5 View Post
You have never initialized a pw object. pw is set to null. Now you are initializing pw in the connectToServer method but perhaps that method isn't called, or it is called and an exception was thrown so the variable wasn't initialized?
... or possibly filenm? Looking at both exceptions I agree it seems increasingly likely that connectToServer isn't being called - which explains the problem with "out" as well.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,649
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 474
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Null pointer Exception

 
0
  #10
Apr 18th, 2009
> I am having a null pointer error, don't know where it came out.

Locating the cause of exceptions is something every Java programmer should be proficient in. Study the stack trace and use a debugger[or debugging statements] to locate the source of problem, *yourself*.
I don't accept change; I don't deserve to live.

Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 559 | Replies: 9
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC