943,670 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 934
  • Java RSS
Apr 17th, 2009
0

Null pointer Exception

Expand Post »
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.
Java Syntax (Toggle Plain Text)
  1. pw.println(filenm);


2.
Java Syntax (Toggle Plain Text)
  1. out.write(buffer,0,bytes);

Thanks a lot in advance.




This is the code:

Java Syntax (Toggle Plain Text)
  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)
Attached Files
File Type: java FTPClient.java (2.2 KB, 15 views)
Last edited by neutralfox; Apr 17th, 2009 at 11:00 am.
Similar Threads
Reputation Points: 6
Solved Threads: 0
Junior Poster
neutralfox is offline Offline
124 posts
since Mar 2009
Apr 17th, 2009
0

Re: Null pointer Exception

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???
Featured Poster
Reputation Points: 1907
Solved Threads: 950
Posting Expert
JamesCherrill is online now Online
5,765 posts
since Apr 2008
Apr 17th, 2009
0

Re: Null pointer Exception

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.
Reputation Points: 6
Solved Threads: 0
Junior Poster
neutralfox is offline Offline
124 posts
since Mar 2009
Apr 17th, 2009
0

Re: Null pointer Exception

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.
Featured Poster
Reputation Points: 1907
Solved Threads: 950
Posting Expert
JamesCherrill is online now Online
5,765 posts
since Apr 2008
Apr 17th, 2009
0

Re: Null pointer Exception

Hello, thanks for the reply but:

They both have been initialized.

Java Syntax (Toggle Plain Text)
  1. byte[] buffer=new byte[1024];
  2. int bytes=0;
Last edited by neutralfox; Apr 17th, 2009 at 4:25 pm.
Reputation Points: 6
Solved Threads: 0
Junior Poster
neutralfox is offline Offline
124 posts
since Mar 2009
Apr 17th, 2009
0

Re: Null pointer Exception

Click to Expand / Collapse  Quote originally posted by neutralfox ...
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.
Featured Poster
Reputation Points: 1907
Solved Threads: 950
Posting Expert
JamesCherrill is online now Online
5,765 posts
since Apr 2008
Apr 17th, 2009
0

Re: Null pointer Exception

Okie , thanks for the fast reply. I will try it. Thanks a lot.
Reputation Points: 6
Solved Threads: 0
Junior Poster
neutralfox is offline Offline
124 posts
since Mar 2009
Apr 17th, 2009
0

Re: Null pointer Exception

From a quick look:

Java Syntax (Toggle Plain Text)
  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?
Reputation Points: 12
Solved Threads: 2
Newbie Poster
chili5 is offline Offline
21 posts
since Dec 2008
Apr 18th, 2009
0

Re: Null pointer Exception

Click to Expand / Collapse  Quote originally posted by chili5 ...
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.
Featured Poster
Reputation Points: 1907
Solved Threads: 950
Posting Expert
JamesCherrill is online now Online
5,765 posts
since Apr 2008
Apr 18th, 2009
0

Re: Null pointer Exception

> 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*.
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: help with 2d array
Next Thread in Java Forum Timeline: Roots of cubic equation





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC