• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Nullpointer Exception

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,I am doing Socket programming for file transfer,for which I wrote code for client and server.But is showing me exception at client side.Can you please help.here is my code

[Added code tags - see UseCodeTags for details]






And Exception Are:



Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at MultiThreadChatClient13.sendFileToServer(MultiThreadChatClient13.java:174)
at MultiThreadChatClient13.actionPerformed(MultiThreadChatClient13.java:158)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2013)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2336)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:260)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:254)
at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:290)
at java.awt.Component.processMouseEvent(Component.java:6100)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3288)
at java.awt.Component.processEvent(Component.java:5865)
at java.awt.Container.processEvent(Container.java:2099)
at java.awt.Component.dispatchEventImpl(Component.java:4461)
at java.awt.Container.dispatchEventImpl(Container.java:2157)
at java.awt.Component.dispatchEvent(Component.java:4287)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4455)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4119)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4049)
at java.awt.Container.dispatchEventImpl(Container.java:2143)
at java.awt.Window.dispatchEventImpl(Window.java:2555)
at java.awt.Component.dispatchEvent(Component.java:4287)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:605)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:276)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:191)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:186)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:178)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:139)
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch
Which line in your code throws that Exception?
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Vaidehi. Welcome to the Ranch!

The only thing that can cause a NullPointerException on that line is if out is null. So it looks like that hasn't been initialised correctly. Did you get any error messages? The way you've written your main() method, it will carry on even if there's an error creating the streams.

I'm a bit dubious about the way your trying to create out, to be honest. Why is it a static variable? And notice that when you finish sendFileToServer you close the stream - that means you won't be able to use it again. It would be better to create a stream when you need it.



(By the way, I've added code tags for you, to make it more readable - see UseCodeTags for details. But it would be even better if your indentation was consistent).
 
vaidehi bhokare
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


On line no.174
 
vaidehi bhokare
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I made some changes in it, here is code:

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.*;
import java.io.PrintStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.text.DecimalFormat;
import java.util.Date;

public class MultiThreadChatClient13 extends JFrame implements Runnable,ActionListener {

private static JButton openB,cancelB,sendB;
private static JLabel fileL,stL,bsL,etL,stlbl,bslbl,etlbl,selectL,sendingL;
private static JFileChooser dialog;
private static Socket clientSocket = null;

private DataInputStream in = null;
private DataOutputStream out = null;
private FileInputStream fis = null;
private String host="192.168.1.102";
public static void main(String[] args)throws IOException {

// The default port.
int portNumber = 2222;
// The default host.
String host = "192.168.1.102";

if (args.length < 2)
{
System.out
.println("Usage: java MultiThreadChatClient <host> <portNumber>\n"
+ "Now using host=" + host + ", portNumber=" + portNumber);
}
else
{
host = args[0];
portNumber = Integer.valueOf(args[1]).intValue();
}

/*
* Open a socket on a given host and port. Open input and output streams.
*/


// if (clientSocket != null && out != null && in != null)
// {
try {
clientSocket = new Socket(host, portNumber);
new Thread(new MultiThreadChatClient13()).start();
}
catch (Exception e)
{
System.err.println("Exception123: " + e);
}

// }
/* else
{
System.out.println("In Else part...");
}*/
}

public void run()
{

try
{
setSize(400,330);
setLayout(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);

selectL=new JLabel("Select File:");
selectL.setBounds(10,10,100,30);

openB=new JButton("Open");
openB.setToolTipText("Open File");
openB.setBounds(120,10,80,30);

fileL=new JLabel("No File Selected");
fileL.setBounds(20,60,350,30);

sendB=new JButton("Send");
sendB.setToolTipText("Send File");
sendB.setBounds(30,100,80,30);
sendB.setEnabled(false);

cancelB=new JButton("Cancel");
cancelB.setToolTipText("Cancel File Selection");
cancelB.setBounds(120,100,80,30);


stlbl=new JLabel("0");
stlbl.setBounds(190,140,200,30);
bslbl=new JLabel("0Bytes");
bslbl.setBounds(190,180,200,30);
etlbl=new JLabel("0");
etlbl.setBounds(190,220,200,30);

stL=new JLabel("Initiation Time: ");
stL.setBounds(30,140,150,30);
bsL=new JLabel("No of Bytes Send: ");
bsL.setBounds(30,180,150,30);
etL=new JLabel("Complition Time: ");
etL.setBounds(30,220,150,30);
sendingL=new JLabel(" ");
sendingL.setBounds(30,260,150,30);

openB.addActionListener(this);
cancelB.addActionListener(this);
sendB.addActionListener(this);

add(selectL);add(openB);
add(fileL);
add(sendB);add(cancelB);
add(stL);add(stlbl);
add(bsL);add(bslbl);
add(etL);add(etlbl);add(sendingL);
show();
}
catch(Exception e)
{
System.out.println("Exception here....");
}


// sendFileToServer(out);

}
public void actionPerformed(ActionEvent ae)
{
try
{
if(ae.getSource()==openB)
{

dialog=new JFileChooser();
dialog.showOpenDialog(this);
fileL.setText(""+dialog.getSelectedFile());
sendB.setEnabled(true);
}
if(ae.getSource()==cancelB)
{
try
{ // clientSocket.close();

}catch(Exception e){System.out.println(e);}
fileL.setText("No File Selected");
stlbl.setText("");
etlbl.setText("");
bslbl.setText("");
sendingL.setText("");
sendB.setEnabled(false);
}

if(ae.getSource()==sendB)
{
try
{
try
{

in = new DataInputStream(new BufferedInputStream(clientSocket.getInputStream()));
out = new DataOutputStream(new BufferedOutputStream(clientSocket.getOutputStream()));
}
catch (UnknownHostException e)
{
System.err.println("Don't know about host " + host);
}
catch (IOException e)
{
System.err.println("Couldn't get I/O for the connection to the host "
+ host);
}

sendFileToServer(out);
}
catch(Exception e)
{
System.out.println("Exception in send button code");
}
}
}
catch(Exception e)
{
System.out.println("Exception in action performed.");
}
}

public void sendFileToServer(DataOutputStream out)
{
try
{

stlbl.setText(" "+new Date());
int totsize=0;
int len=0;
String fname=dialog.getSelectedFile().getName();
System.out.println("client : file name: " +fname);
// String suffix=fname.substring(fname.lastIndexOf('/')+1,fname.length()).trim();

out.writeUTF(fname);
out.flush();

int fileLong = new Integer((int)dialog.getSelectedFile().length());
System.out.println("Size:"+fileLong);
byte[] buf=new byte[12837696];
System.out.println("Size1:"+fileLong);
out.writeUTF("get");

out.writeInt(fileLong);
out.flush();

System.out.println("File name send");

fis=new FileInputStream(dialog.getSelectedFile());
System.out.println("File Open");
while((len=fis.read(buf))!=-1)
{
totsize+=len;
//System.out.println(buf);
bslbl.setText(""+totsize);
out.write(buf,0,len);
}
etlbl.setText(""+new Date());
fis.close();
out.close();
in.close();
clientSocket.close();
DecimalFormat df= new DecimalFormat("#0.00 KB");
bslbl.setText(" "+df.format(totsize/1024d));
}
catch (IOException e)
{
System.out.println("Exception in sendFile Server");
e.printStackTrace();
}
}
}// Java Document



But now it shows the exception like:
Exception123: java.net.ConnectException: Connection timed out: connect
the client is at my end and server code is on another end.what is real issue regarding this?
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Without code tags we cannot tell which line number you meant. Please go back and use the button.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Vaidehi. Can you format any code you post, as I mentioned? Also, you don't need to post that much, just the bits relevant to your problem. Thanks.

I'm going to move this to the networking forum, because the problem isn't really related to Swing and you might get more specific help there. I can't really say what your problem is here, but the first thing I'd be looking to check is that the server is actually running and listening to the address you're connecting to.
 
vaidehi bhokare
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you so much, I found the problem it is related with IP address.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic