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.
pw.println(filenm);
2.
out.write(buffer,0,bytes);
Thanks a lot in advance.
This is the code:
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)