This week's book giveaway is in the Design and Architecture forum.
We're giving away four copies of Communication Patterns: A Guide for Developers and Architects and have Jacqui Read on-line!
See this thread for details.
  • 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Null Pointer Exception

 
Ranch Hand
Posts: 273
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is causing the null point exception? The system says line 221 which is

Here is the method and can post more code if you need.

 
Bartender
Posts: 1166
17
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My best guess is that 'connect' is null and though you test for null and print an error message you don't actually abort when 'connect' is null. Since we can't see the rest of your code it is impossible to say why 'connect' is null.
 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The source for NullPointerExceptions are when doing a .(dot) operation on a method with an object that is null.
 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From the code "connect" is null. To avoid this you can either do:



Or



Now the question is where and when you initialize "connect"?
 
Richard Tookey
Bartender
Posts: 1166
17
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

K. Tsang wrote:From the code "connect" is null. To avoid this you can either do:



Or



Now the question is where and when you initialize "connect"?



The chances are that 'connect' being null represents a system error so I would no neither of these and would in fact remove the null check altogether and just let any NPE propagate back to the caller. What I would do is extract the connect.getInputStream() as a separate line then the cause of the NPE would be easier to diagnose.
 
Charles Sexton
Ranch Hand
Posts: 273
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Still having issues with the NPE!!!

Here are my two classes, each is in a separate project and the project is a basic client server app where the server reads from two files based on the requests of the client. This application is not finished and still needs work as you can see so please feel free to comment on whatever you please. Thank You!






 
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Is not going to stop the null pointer. You must not do connect.getInputStream() if connect is null.
 
Richard Tookey
Bartender
Posts: 1166
17
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't have access to your main() methods but I can see no path through the client that actually calls runClient() which is needed to connect to the server so 'connect' is always null.

P.S. Don't throw debug information away by converting exceptions to simple System.err.println("some error message"). Print the stack trace!
 
Charles Sexton
Ranch Hand
Posts: 273
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Richard Tookey wrote:I don't have access to your main() methods but I can see no path through the client that actually calls runClient() which is needed to connect to the server so 'connect' is always null.

P.S. Don't throw debug information away by converting exceptions to simple System.err.println("some error message"). Print the stack trace!



Hey Richard, I just did the System.out.println(); to pin point the methods. I will eventually take them out and use ex.printStackTrace();

Also the main method calls runClient(); through an object.
 
Charles Sexton
Ranch Hand
Posts: 273
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

E Armitage wrote:

Is not going to stop the null pointer. You must not do connect.getInputStream() if connect is null.



Would you suggest system.exit(0); ?
 
Richard Tookey
Bartender
Posts: 1166
17
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Charles Sexton wrote:

Richard Tookey wrote:I don't have access to your main() methods but I can see no path through the client that actually calls runClient() which is needed to connect to the server so 'connect' is always null.

P.S. Don't throw debug information away by converting exceptions to simple System.err.println("some error message"). Print the stack trace!



Hey Richard, I just did the System.out.println(); to pin point the methods. I will eventually take them out and use ex.printStackTrace();


You have it back to front! You need the stack trace while you are in your current state i.e. debugging! In this situation you get far more information out of a stack trace than you will ever get from a System.out !



Also the main method calls runClient(); through an object.



Which of course we can't verify that it is done correctly since you have not posted the main methods. The fact is 'connect' is most probably null so you have to verify that you have called runClient() on the correct object before the GUI is constructed. We can't do that since we don't have access to the code that is supposed to do it!
 
Charles Sexton
Ranch Hand
Posts: 273
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Richard Tookey wrote:

Charles Sexton wrote:

Richard Tookey wrote:I don't have access to your main() methods but I can see no path through the client that actually calls runClient() which is needed to connect to the server so 'connect' is always null.

P.S. Don't throw debug information away by converting exceptions to simple System.err.println("some error message"). Print the stack trace!



Hey Richard, I just did the System.out.println(); to pin point the methods. I will eventually take them out and use ex.printStackTrace();


You have it back to front! You need the stack trace while you are in your current state i.e. debugging! In this situation you get far more information out of a stack trace than you will ever get from a System.out !



Also the main method calls runClient(); through an object.



Which of course we can't verify that it is done correctly since you have not posted the main methods. The fact is 'connect' is most probably null so you have to verify that you have called runClient() on the correct object before the GUI is constructed. We can't do that since we don't have access to the code that is supposed to do it!



Here are the main classes and the two that I updated with errors.

Thank You



package sexton_it351_p1ip_client;

import java.io.IOException;
import javax.swing.JFrame;

public class Sexton_IT351_P1IP_Client {

///LOOK If main can throw an uncaught exception, then you lose points because I WILL crash your program
public static void main(String[] args) throws IOException {



JFrame window = new JFrame("Client");
//Add to window new SalesPanel
window.add(new Client());
//Set window default close operation to JFrame exit on close
window.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); ///LOOK WHY DISABLE THE 'Quit' BUTTON ??
//Set window visibility true
window.setVisible(true);
//Set window bounds to 800,800,600,550
window.setBounds(800, 800, 600, 550);
//Set window set location 700, 300
window.setLocation(700, 300);

Client app = new Client();
app.runClient();

}
}


package sexton_it351_p1ip_server;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
*
* @author CharlesSexton
*/
public class Server {

private ServerSocket server;
private ObjectOutputStream output;
private ObjectInputStream input;
private Socket connect;

public void runServer() {
try {
server = new ServerSocket(9999, 25);
while (true) {

waitConnection();
getStreams();
processConnection();

}
} catch (IOException ex) {
ex.printStackTrace();
}
}

public void waitConnection() {
try {
connect = server.accept();

System.out.println(connect.getLocalAddress());
System.out.println(connect.getPort());
} catch (IOException ex) {
ex.printStackTrace();
}
}

public void getStreams() {
try {

input = new ObjectInputStream(connect.getInputStream());
output.flush();
output = new ObjectOutputStream(connect.getOutputStream());
} catch (IOException ex) {
ex.printStackTrace();
}
}

public void processConnection() throws IOException {

getStreams();




do { // process messages sent from server
try { // read message and display it
String input2; // display message
input2 = (String) input.readObject();

System.out.println(input2);
} // end try
catch (ClassNotFoundException ex) {
ex.printStackTrace();
} // end catch
catch (java.net.SocketException ex) {
ex.printStackTrace();
break;
}
}while(input != null);


}

public void closeConnection() {

getStreams();

try {
input.close();
output.close();


connect.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}


package sexton_it351_p1ip_server;

import java.io.IOException;

public class Sexton_IT351_P1IP_Server {

public static void main(String[] args) throws IOException {
Server app = new Server();
app.runServer();
}
}



PRINT STACK TRACE!!! When ran and Customer display button is pushed with dddd in the correct JTextField


Server Project/Application

java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:189)
at java.net.SocketInputStream.read(SocketInputStream.java:121)
at java.io.ObjectInputStream$PeekInputStream.read(ObjectInputStream.java:2308)
at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2321)
at java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:2792)
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:799)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:299)
at sexton_it351_p1ip_server.Server.getStreams(Server.java:49)
at sexton_it351_p1ip_server.Server.runServer(Server.java:26)
at sexton_it351_p1ip_server.Sexton_IT351_P1IP_Server.main(Sexton_IT351_P1IP_Server.java:9)
java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:134)
at java.net.SocketInputStream.read(SocketInputStream.java:121)
at java.io.ObjectInputStream$PeekInputStream.read(ObjectInputStream.java:2308)
at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2321)
at java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:2792)
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:799)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:299)
at sexton_it351_p1ip_server.Server.getStreams(Server.java:49)
at sexton_it351_p1ip_server.Server.processConnection(Server.java:59)
at sexton_it351_p1ip_server.Server.runServer(Server.java:27)
at sexton_it351_p1ip_server.Sexton_IT351_P1IP_Server.main(Sexton_IT351_P1IP_Server.java:9)
Exception in thread "main" java.lang.NullPointerException
at sexton_it351_p1ip_server.Server.processConnection(Server.java:67)
at sexton_it351_p1ip_server.Server.runServer(Server.java:27)
at sexton_it351_p1ip_server.Sexton_IT351_P1IP_Server.main(Sexton_IT351_P1IP_Server.java:9)
Java Result: 1
BUILD SUCCESSFUL (total time: 7 seconds)



Print Stack Trace for Client Project/Application

getStreams. connect is NULL *****

BUILD SUCCESSFUL (total time: 3 seconds)








 
Charles Sexton
Ranch Hand
Posts: 273
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I apologize for the forum rules error.....



Client Application main method repost due to error.....






Main Method for Server Application repost due to error.......





Server Class within Server Application repost due to error......


 
Charles Sexton
Ranch Hand
Posts: 273
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also please take a look at the close button and the customerDisplay button......they are both related to the input/output streams and I haven't even started on displayAll button and productButton......the application will eventually read from txt files.....just trying to establish a firm connect using the close and customer display buttons.....without errors.....but I keep running into lots of errors and having to repeatedly change things.
 
Richard Tookey
Bartender
Posts: 1166
17
Netbeans IDE Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For some reason that totally escapes me your client code creates 4 instances of class Client but only applies the runClient() on one of them so 3 of the clients will never have made a connection to the server! I suspect you should stand back and review your design because your client code looks to be creating too many instances of your client and, since it is single threaded, your server code can handle only one client at a time.

Your big-bang approach rarely works. You should start with a much more simple client-server program. The server should be able to handle multiple simultaneous connections and the client should be able to handle multiple request/response actions. Initially there should be no GUI. Until you have the simple system working and you fully understand the concepts you are probably wasting your time trying anything a complex as your current work.

In my view you should really spend a lot of time studying http://docs.oracle.com/javase/tutorial/networking/sockets/clientServer.html and in particular the "Supporting Multiple Clients" section.
 
Charles Sexton
Ranch Hand
Posts: 273
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Richard Tookey wrote:For some reason that totally escapes me your client code creates 4 instances of class Client but only applies the runClient() on one of them so 3 of the clients will never have made a connection to the server! I suspect you should stand back and review your design because your client code looks to be creating too many instances of your client and, since it is single threaded, your server code can handle only one client at a time.

Your big-bang approach rarely works. You should start with a much more simple client-server program. The server should be able to handle multiple simultaneous connections and the client should be able to handle multiple request/response actions. Initially there should be no GUI. Until you have the simple system working and you fully understand the concepts you are probably wasting your time trying anything a complex as your current work.

In my view you should really spend a lot of time studying http://docs.oracle.com/javase/tutorial/networking/sockets/clientServer.html and in particular the "Supporting Multiple Clients" section.



This is a past due school assignment in which I am trying to get help in and I assume you are saying start over with something that just writes to the system?
 
Richard Tookey
Bartender
Posts: 1166
17
Netbeans IDE Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Charles Sexton wrote:
This is a past due school assignment in which I am trying to get help in


As was said on day one of this thread, 'connect' is null and you need to address that problem and the only way I can see to address that is to address the fundamental design issue. Since you are already in extra time I can't see that I can help further without actually re-writing the code for you which would not really help you.


and I assume you are saying start over with something that just writes to the system?



In effect yes but your time constraints make that sound impossible.
 
Charles Sexton
Ranch Hand
Posts: 273
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Richard Tookey wrote:

Charles Sexton wrote:
This is a past due school assignment in which I am trying to get help in


As was said on day one of this thread, 'connect' is null and you need to address that problem and the only way I can see to address that is to address the fundamental design issue. Since you are already in extra time I can't see that I can help further without actually re-writing the code for you which would not really help you.


and I assume you are saying start over with something that just writes to the system?



In effect yes but your time constraints make that sound impossible.




Thank you, sounds like I have no choice.....
 
I suggest huckleberry pie. But the only thing on the gluten free menu is this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic