| | |
Null pointer Exception
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Mar 2009
Posts: 123
Reputation:
Solved Threads: 0
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.
2.
Thanks a lot in advance.
This is the code:
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)
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)
pw.println(filenm);
2.
Java Syntax (Toggle Plain Text)
out.write(buffer,0,bytes);
Thanks a lot in advance.
This is the code:
Java Syntax (Toggle Plain Text)
import java.net.*; import java.io.*; import javax.swing.JOptionPane; public class FTPClient implements Runnable { Socket s; long length; File file; InputStreamReader in = null; OutputStream out = null; BufferedReader br = null; PrintWriter pw = null; String filenm = null; String fn; public FTPClient(){ } public FTPClient(int port){ connectToServer(port); new Thread( this ).start(); } //connect to the server public void connectToServer(int port){ try{ s = new Socket ("127.0.0.1" , port); br = new BufferedReader(new InputStreamReader(s.getInputStream())); pw = new PrintWriter(s.getOutputStream(),true); out = s.getOutputStream(); JOptionPane.showMessageDialog(null, "Client says: ... I am connected and IO Ready!"); }catch(Exception e){e.printStackTrace();} } //send bytes to server public void sendBytes(File f){ try{ JOptionPane.showMessageDialog(null, "Client says: File send ...."); JOptionPane.showMessageDialog(null, "Client says: The file name: " + f.getName()); JOptionPane.showMessageDialog(null, "Client says: The file size: " + f.length()); filenm = f.getName(); pw.println(filenm); // got a java.lang.NullPointerException here FileInputStream fis = new FileInputStream(filenm); byte[] buffer=new byte[1024]; int bytes=0; while((bytes=fis.read(buffer))!=-1) { out.write(buffer,0,bytes); // got a java.lang.NullPointerException here also when I comment // pw.println(filenm); the first one JOptionPane.showMessageDialog(null, "Test .... "); } fis.close(); }catch(Exception e){e.printStackTrace();} } }
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.
•
•
Join Date: Mar 2009
Posts: 123
Reputation:
Solved Threads: 0
Hello, thanks for the reply but:
They both have been initialized.
They both have been initialized.
Java Syntax (Toggle Plain Text)
byte[] buffer=new byte[1024]; int bytes=0;
Last edited by neutralfox; Apr 17th, 2009 at 4:25 pm.
•
•
Join Date: Apr 2008
Posts: 1,026
Reputation:
Solved Threads: 151
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.
From a quick look:
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?
Java Syntax (Toggle Plain Text)
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?
•
•
Join Date: Apr 2008
Posts: 1,026
Reputation:
Solved Threads: 151
... 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.
> 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*.
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
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
![]() |
Similar Threads
- Getting a null pointer exception when saving image to file. (Java)
- Null Pointer Exception problem (Java)
- Null Pointer Exception....... (Java)
- Null pointer exception while getting other applet in the same page (Java)
- null pointer exception --- urgent (Java)
- Java Null Pointer Exception (Java)
- help with sort using Calendar class getting null pointer exception (Java)
Other Threads in the Java Forum
- Previous Thread: help with 2d array
- Next Thread: Roots of cubic equation
Views: 559 | Replies: 9
| Thread Tools | Search this Thread |
Tag cloud for Java
account android api apple applet application arguments array arrays automation binary bluetooth chat class classes client code columns component data database draw eclipse error event exception expand file filechooser fractal game givemetehcodez google graphics gui helpwithhomework homework html ide image inheritance input integer j2me java javaprojects jlabel jme jmf jni jpanel jtextfield julia linux list loop map method methods midlethttpconnection mobile monitoring netbeans newbie nullpointerexception number object open-source oracle print problem program programming project property recursion ria scanner screen search server set size sms socket sort sourcelabs splash sql sqlite static string swing test testautomation threads time transfer tree windows






