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)

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???

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.

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.

Hello, thanks for the reply but:

They both have been initialized.

byte[] buffer=new byte[1024];
            int bytes=0;

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.

Okie , thanks for the fast reply. I will try it. Thanks a lot.

From a quick look:

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?

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.

> 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*.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.