Results 1 to 12 of 12

Thread: Exception

  1. #1
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default Exception

    Hey.. I get
    Java Code:
    Exception in thread "Thread-4" java.lang.NullPointerException
    	at org.square.Client.CreateApplet.run(CreateApplet.java:49)
    	at java.lang.Thread.run(Unknown Source)
    this is my CreateApplet class:
    Java Code:
    package org.square.Client;
    
    import java.awt.Color;
    import java.awt.Graphics;
    import java.io.*;
    import java.net.*;
    
    import javax.swing.JApplet;
    
    public class CreateApplet extends JApplet implements Runnable{
    	
    	int x;
    	int y;
    	
    	public void init(){
    		setSize(500, 500);
    		connect();
    	}
    	
    	public void paint(Graphics g){
    		CreateSquare(g);
    	}
    	
    	DataOutputStream os;
    	DataInputStream is;
    	BufferedReader br;
    	Socket s;
    	
    	public void connect(){
    		try{
    			s = new Socket("localhost", 44444);
    			is = new DataInputStream(s.getInputStream());
    			os = new DataOutputStream(s.getOutputStream());
    		}catch(IOException e){
    			e.printStackTrace();
    		}
    		
    		if (s != null && os != null && is != null) {
    			new Thread(new CreateApplet()).start();
    			
    		}
    	}
    	
    	public String color;
    
    	@Override
    	public void run() {
    		try{
    		    System.out.println(is.read());
    		} catch (IOException e) {
    		    System.err.println("IOException:  " + e);
    		   
    		}
    	}
    	
    	public enum colors{
    		black, red, yellow, blue, noColor;
    	}
    	
    	public colors getColors(){
    		if(color == null)
    			return colors.noColor;
    		if(color.equals("black")){
    			return colors.black;
    		}
    		if(color.equals("red")){
    			return colors.red;
    		}
    		if(color.equals("yellow")){
    			return colors.yellow;
    		}
    		if(color.equals("blue")){
    			return colors.blue;
    		}
    		
    		return colors.noColor;
    	}
    	
    	public void CreateSquare(Graphics g){
    		switch(getColors()){
    		case noColor:
    			break;
    			
    		case black:
    			g.setColor(Color.black);
    			g.fillRect(x, y, 50, 50);
    			break;
    			
    		case red:
    			g.setColor(Color.red);
    			g.fillRect(x, y, 50, 50);
    			break;
    			
    		case yellow:
    			g.setColor(Color.yellow);
    			g.fillRect(x, y, 50, 50);
    			break;
    			
    		case blue:
    			g.setColor(Color.blue);
    			g.fillRect(x, y, 50, 50);
    			break;
    		}
    		
    		
    	}
    	
    	
    
    }
    how can I fix it?

    if you will need my server class just ask.

  2. #2
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default

    Another question..
    Could anyone tell me what kind of security stuff could I add to the game?
    For example encrypting for passwords and how can I encrypt my passwords, and how can I secure the connection from hackers?

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,418
    Rep Power
    25

    Default

    CreateApplet.java:49
    What object at line 49 is null? Why is it null?
    Give it a value before trying to use it.

  4. #4
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default

    Quote Originally Posted by Norm View Post
    CreateApplet.java:49
    What object at line 49 is null? Why is it null?
    Give it a value before trying to use it.
    How can I give a value to :
    Java Code:
    System.out.println(is.read());

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,418
    Rep Power
    25

    Default

    How can I give a value to :
    To what? You didn't show any variable in your message.

    The code that you posted has the variable "is".
    Is it null? Add a println() to show the value of 'is' before you use it.

    Your problem is multiple copies of an object.

  6. #6
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default

    Quote Originally Posted by Norm View Post
    To what? You didn't show any variable in your message.

    The code that you posted has the variable "is".
    Is it null? Add a println() to show the value of 'is' before you use it.

    Your problem is multiple copies of an object.
    Yes, it is a null, and what do you mean by multiple copies of an object?

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,418
    Rep Power
    25

    Default

    new CreateApplet()
    The above creates a new instance of CreateApplet
    so now there are two instances, one with your data and one without (ie with null values)

  8. #8
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default

    Quote Originally Posted by Norm View Post
    new CreateApplet()
    The above creates a new instance of CreateApplet
    so now there are two instances, one with your data and one without (ie with null values)
    Yay, it works.


    Another question..
    Could you tell me what kind of security stuff could I add to the game?
    For example encrypting for passwords and how can I encrypt my passwords, and how can I secure the connection from hackers?

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,418
    Rep Power
    25

    Default

    How is the applet loaded and executed? If from a server, you can control access to the password file there.
    There are java classes for encryption.

  10. #10
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default

    Quote Originally Posted by Norm View Post
    How is the applet loaded and executed? If from a server, you can control access to the password file there.
    There are java classes for encryption.
    Would it be safe to encrypt the password twice with md5 encryption? Because then the hackers cant use the md5decryption websites to get the password?

  11. #11
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,418
    Rep Power
    25

    Default

    Sorry I've never used encryption. What hackers are you worried about?

    I'll have to do some research on encryption.

    I wonder if the server generates a unique key and passes it with each applet that is downloaded to a client and the applet used that key for its encryption, would that be sufficient. The key would only be valid for the life of the session. If key were intercepted,
    it wouldn't be any use for anyone at a later time. The hole in this is if someone can see all the traffic, then he'd have the key and the encrypted password and the code that generated the encrypted password. From that he might be able to compute the password.

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default

    MD5 isn't encryption, because you can't decrypt it. It's a hash function. Double hashing won't help since the "decrypt" simply looks for something that gives the same hashcode, which double hashing does not affect.

    The current recommended hashing function is SHA (2?), which I'm sure has libraries around.

Similar Threads

  1. Replies: 4
    Last Post: 10-25-2010, 07:42 PM
  2. exception
    By thamizhisai in forum Advanced Java
    Replies: 9
    Last Post: 05-30-2008, 08:47 AM
  3. why this exception
    By payal.mitra86 in forum JDBC
    Replies: 1
    Last Post: 05-21-2008, 10:28 PM
  4. exception
    By Oktam in forum New To Java
    Replies: 2
    Last Post: 03-23-2008, 07:01 PM
  5. Replies: 1
    Last Post: 03-08-2008, 06:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •