Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (580)
games submitted by our members
Games in WIP (500)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1] 2
  ignore  |  Print  
  Exception in thread "main" java.lang.NullPointerException  (Read 6632 times)
0 Members and 1 Guest are viewing this topic.
Offline harry@dayfamilyweb

Junior Member





« Posted 2006-04-25 09:35:15 »

i am having the problem below
Exception in thread "main" java.lang.NullPointerException
        at org.HSim.net.HSIP.server.HSIPProtocolHandler.checkInput(HSIPProtocolHandler.java:70)
        at org.HSim.net.HSIP.server.HSIPProtocolHandler.<init>(HSIPProtocolHandler.java:39)
        at org.HSim.net.HSIP.server.HSIPrunner.Start(HSIPrunner.java:31)
        at testofhsip.Main.main(Main.java:30)
Java Result: 1
I am using MINA
1. at testofhsip.Main.main
/a simple app to test the API i am making on top of MINA
public static void main(String[] args) throws IOException {
        HSIPrunner s = new HSIPrunner();
        s.Start();
        char[] r = "Hi".toCharArray();       
        s.write(r);
        // TODO code application logic here
    }
2. at org.HSim.net.HSIP.server.HSIPrunner.Start(HSIPrunner.java:31)
HSIP = new HSIPProtocolHandler();
3.
at org.HSim.net.HSIP.server.HSIPProtocolHandler.<init>(HSIPProtocolHandler.java:39)
 try {
            char[] C = null;
            checkInput(IS , C);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
4. at org.HSim.net.HSIP.server.HSIPProtocolHandler.checkInput(HSIPProtocolHandler.java:70)
char[] CB = C;
       reader.read(CB , 0 , 20);
note:
1. reader is:
InputStreamReader reader;
intilzed:
rotected void processStreamIo(IoSession ioSession, InputStream inputStream, OutputStream outputStream) {
       try {
            reader = new InputStreamReader(inputStream , "ISO-8859-1");
            OSW = new OutputStreamWriter(outputStream , "ISO-8859-1");
           
       } catch(UnsupportedEncodingException e) {
           
       }
       
    }
if someone could help i would be gratefull
Offline Ask_Hjorth_Larsen

Junior Member




Java games rock!


« Reply #1 - Posted 2006-04-25 18:14:57 »

You call checkInput with a char[] which is null.

Rather fishy isn't it?
Offline harry@dayfamilyweb

Junior Member





« Reply #2 - Posted 2006-04-25 23:48:24 »

You call checkInput with a char[] which is null.

Rather fishy isn't it?
what do you mean checkinput intilizez the char by calling
reader.read(CB , 0 , 20):
which intilzese the char Grin i do not get why this exception is thrown
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Kova

Senior Member





« Reply #3 - Posted 2006-04-26 00:34:23 »

try little debuging...
put if x==null and then print out "is null" to check if it's reall null when it reaches that line
NullExceptions are my favorite exceptions Smiley they are really easy to fix Wink
Offline harry@dayfamilyweb

Junior Member





« Reply #4 - Posted 2006-04-26 01:34:32 »

it still dose not work it dose not print CB null which is what i told it to if its null But the exception continues Exception in thread "main" java.lang.NullPointerException
        at org.HSim.net.HSIP.server.HSIPProtocolHandler.checkInput(HSIPProtocolHandler.java:73)
        at org.HSim.net.HSIP.server.HSIPProtocolHandler.<init>(HSIPProtocolHandler.java:39)
        at org.HSim.net.HSIP.server.HSIPrunner.Start(HSIPrunner.java:31)
        at testofhsip.Main.main(Main.java:30)
Java Result: 1

PS i am using 1.6.0b81
also isn`t CB suposed to be null becose reader.read(CB , 0 ,20);
intlizes it
PS reader.read(CB , 0 , 20);
is InputStreamReader.read(CB , 0 , 20);
Offline harry@dayfamilyweb

Junior Member





« Reply #5 - Posted 2006-04-26 01:45:15 »

this is my new code it still dose not work
 at org.HSim.net.HSIP.server.HSIPProtocolHandler.checkInput(HSIPProtocolHandler.java:73)
public void checkInput(String IS) throws IOException {
       char[] CB = new char[20];
       if(CB == null) {
           System.out.println("CB null");
       }
       reader.read(CB , 0 , 20);
        at org.HSim.net.HSIP.server.HSIPProtocolHandler.<init>(HSIPProtocolHandler.java:39)
    public HSIPProtocolHandler() {
        super();
        String IS = null;
        try {
             
            checkInput(IS);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
        at org.HSim.net.HSIP.server.HSIPrunner.Start(HSIPrunner.java:31)
public void Start() throws IOException {
       
        HSIP = new HSIPProtocolHandler();
        SimpleServiceRegistry registry = new SimpleServiceRegistry();
       
        Service service = new Service("HSIP" , TransportType.SOCKET , PORT );
        registry.bind(service , HSIP);
       
    }
        at testofhsip.Main.main(Main.java:30)
public static void main(String[] args) throws IOException {
        HSIPrunner s = new HSIPrunner();
        s.Start();
        char[] r = "Hi".toCharArray();       
        s.write(r);
        // TODO code application logic here
    }
PS org.HSim.net.HSIP.server.HSIPrunner
is a thread
Offline swpalmer

JGO Coder




Where's the Kaboom?


« Reply #6 - Posted 2006-04-26 02:27:46 »

Could you post a proper stack trace with a clean code example?

The mix of stack trace and code all mashed together makes it very hard to read.

Offline Kova

Senior Member





« Reply #7 - Posted 2006-04-26 02:56:37 »

and please mark the line in code where nullexception is thrown
Offline harry@dayfamilyweb

Junior Member





« Reply #8 - Posted 2006-04-26 04:34:13 »

init:
deps-jar:
compile:
run:
Exception in thread "main" java.lang.NullPointerException
        at org.HSim.net.HSIP.server.HSIPProtocolHandler.checkInput(HSIPProtocolHandler.java:73)
        at org.HSim.net.HSIP.server.HSIPProtocolHandler.<init>(HSIPProtocolHandler.java:39)
        at org.HSim.net.HSIP.server.HSIPrunner.Start(HSIPrunner.java:31)
        at testofhsip.Main.main(Main.java:30)
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
package testofhsip;

import java.io.IOException;
import org.HSim.net.HSIP.server.HSIPrunner;

/**
 *
 * @author HandE
 */

public class Main {
   
    /** Creates a new instance of Main */
    public Main() {
    }
   
    /**
     * @param args the command line arguments
     */

    public static void main(String[] args) throws IOException {
        HSIPrunner s = new HSIPrunner();
        s.Start();
        char[] r = "Hi".toCharArray();        
        s.write(r);
        // TODO code application logic here
   }
   
}

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
package org.HSim.net.HSIP.server;

import java.io.IOException;
import java.util.logging.Logger;
import org.apache.mina.common.TransportType;
import org.apache.mina.registry.Service;
import org.apache.mina.registry.ServiceRegistry;
import org.apache.mina.registry.SimpleServiceRegistry;

/**
 *
 * @author HandE
 */

public class HSIPrunner extends Thread {
    private static final int PORT = 4444;
    private static Logger log =
       Logger.getLogger("java.net.HSIP");
    HSIPProtocolHandler HSIP;
    /** Starts the Server */
    public void Start() throws IOException {
       
        HSIP = new HSIPProtocolHandler();
        SimpleServiceRegistry registry = new SimpleServiceRegistry();
       
        Service service = new Service("HSIP" , TransportType.SOCKET , PORT );
        registry.bind(service , HSIP);
       
    }
 public void run() {
        //checks Input then sleeps for 100 mileseconds
       String IS = null;
        try {
           
            while(run = true) {
                HSIP.checkInput(IS);
                try {
                    if(HSIP.MaxUserNOReached = true) {
                        maxNO();
                        full = HSIP.MaxUserNOReached;
                    }
                    this.sleep(100);
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
            }
           
           
        } catch (IOException ex) {
            ex.printStackTrace();
        }
       
    }

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
public class HSIPProtocolHandler extends StreamIoHandler {
   
    private static Logger log =
       Logger.getLogger("hsipserver");
    private int userCount;
    /** Creates a new instance of HSIPProtocolHandler */
    public HSIPProtocolHandler() {
        super();
        String IS = null;
        try {
             
            checkInput(IS);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
InputStreamReader reader;
protected void processStreamIo(IoSession ioSession, InputStream inputStream, OutputStream outputStream) {
       try {
            reader = new InputStreamReader(inputStream , "ISO-8859-1");
            OSW = new OutputStreamWriter(outputStream , "ISO-8859-1");
           
       } catch(UnsupportedEncodingException e) {
           
       }
       
    }
public void checkInput(String IS) throws IOException {
       char[] CB = new char[20];
       if(CB == null) {
           System.out.println("CB null");
       }
      //line below throws Exception
     [u]reader.read(CB , 0 , 20);[/u]
       char[] Local = CB;
       String pass = String.valueOf(CB , 20 , 7);
       String local = null;
       local = local.valueOf(Local , 0 , 1);
       
       if(local == "H") {
          userCount += 1;
          char[] name = null;
          String Name = local.valueOf(name , 1 , 10);
          newUser(userCount , Name , pass);    
         
       }
       
       IS.valueOf(CB);
    }

Offline kevglass

JGO Kernel


Medals: 85
Projects: 25


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #9 - Posted 2006-04-26 04:58:34 »

It appears "reader" is null.

Kev

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline swpalmer

JGO Coder




Where's the Kaboom?


« Reply #10 - Posted 2006-04-26 05:11:43 »

yeah, when does processStreamIo get called?

Offline kevglass

JGO Kernel


Medals: 85
Projects: 25


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #11 - Posted 2006-04-26 05:27:30 »

Looks like its part of MINA, I'd suspect that empty catch{} block myself. What happens if that explcit encoding doesn't work?

Kev

Offline swpalmer

JGO Coder




Where's the Kaboom?


« Reply #12 - Posted 2006-04-26 05:34:55 »

Sigh... empty catch block... not even a comment in it...  tsk.  It's like you want the problems to be hidden.

Offline kevglass

JGO Kernel


Medals: 85
Projects: 25


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #13 - Posted 2006-04-26 05:43:28 »

Getting a bit disappointed with this recently to be honest, I mean is this a java gaming forum or a how to program forum?

Kev

Offline swpalmer

JGO Coder




Where's the Kaboom?


« Reply #14 - Posted 2006-04-26 05:56:47 »

You've noticed it too then...

I think it is ultimately a good sign for Java gaming...  this place is getting more popular... more people are considering Java for games.  But it does mean that there are going to be lots of newbies.

I'm not sure there is anything we can do to keep the conversations at the releatively high level that we have been acustomed to.  Other than make sure the moderators keep the newbie posts in the appropriate forum.

Anyway, everyone has to learn somehow...  I guess the veterans here have the responsibility of providing guidance.

Offline harry@dayfamilyweb

Junior Member





« Reply #15 - Posted 2006-04-26 09:33:13 »

 Huh Huh
Offline CaptainJester

JGO Knight


Medals: 12
Projects: 2


Make it work; make it better.


« Reply #16 - Posted 2006-04-26 13:39:09 »

You are hiding your exception.  When you do that you are just asking for trouble.
1  
2  
3  
4  
5  
6  
7  
8  
9  
  protected void processStreamIo(IoSession ioSession, InputStream inputStream, OutputStream outputStream) {
    try {
      reader = new InputStreamReader(inputStream , "ISO-8859-1");
      OSW = new OutputStreamWriter(outputStream , "ISO-8859-1");
           
    } catch(UnsupportedEncodingException e) {
           
    }
  }

You should ALWAYS at the very least call
e.printStackTrace();

What is probably happening is this method is throwing an exception, which causes reader to not be initialized.  You don't know it because you swallow/hide the exception.

Offline harry@dayfamilyweb

Junior Member





« Reply #17 - Posted 2006-04-27 02:17:22 »

ok i am just going to scrap that code MINA is not that good but i`ve got a nother one in my code now  Angry Sad
init:
deps-jar:
compile:
run:
startting...
got output stream
Exception in thread "main" java.lang.NullPointerException
        at org.HSim.net.HSIP.server.ManServe.writeString(ManServe.java:131)
        at testofhsip.Main.main(Main.java:30)
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)
1  
2  
3  
4  
5  
6  
7  
8  
9  
 
public void writeString(String infotosend) throws NotStarted, IOException {
        if(started = true) {
            OSW.write(infotosend);
        } else {
            throw new NotStarted();
        }
       
    }

OSW is not null it is intalized in
org.HSim.net.HSIP.server.ManServe.run() before
write can be called at
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
 
public void run() {
        try {
            h = serverSocket.accept();
           
            System.out.println("starting ....");
            log.finest("startting");
            h.setPerformancePreferences(2 , 0 , 1);
            h.setTcpNoDelay(true);
            h.setKeepAlive(true);
            h.setOOBInline(true);
            h.setSendBufferSize(2000);
            System.out.println("configuring.......");
            log.finest("configuring");
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        OutputStream();

at
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
private void OutputStream() {
        try {
            OSW = new OutputStreamWriter(h.getOutputStream() , "ISO-8859-1");
            started = true;
        } catch (UnsupportedEncodingException ex) {
            ex.printStackTrace();
            log.warning(ex.getMessage());
        } catch (IOException ex) {
            ex.printStackTrace();
            log.warning(ex.getMessage());
        }
    }

Offline Kova

Senior Member





« Reply #18 - Posted 2006-04-27 10:02:55 »

1  
OSW.write(infotosend);


that's line 131? Something in that line is null. It dosen't have to be OSW, it can be infotosend. As before, just test it for null before just before that line.
Offline harry@dayfamilyweb

Junior Member





« Reply #19 - Posted 2006-04-27 10:48:17 »

 OSW = new OutputStreamWriter(h.getOutputStream() , "ISO-8859-1");
note h is a soket
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
public class Main {
   
    /** Creates a new instance of Main */
    public Main() {
    }
   
    /**
     * @param args the command line arguments
     */

    public static void main(String[] args) throws IOException {
        ManServe s = new ManServe();
        s.start();
        try {
            s.writeString("hi");
        } catch (IOException ex) {
            ex.printStackTrace();
        } catch (NotStarted ex) {
            ex.printStackTrace();
        }
        s.close();
        // TODO code application logic here
   }
   
}

s.writeString("hi");
is infotosend = "hi" Shocked Sad Angry Embarrassed Cry Cry Cry Cry Cry Cry
Online Riven
« League of Dukes »

JGO Overlord


Medals: 605
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #20 - Posted 2006-04-27 11:34:26 »

Probably not related to your problem... but:

1  
2  
3  
4  
if(started = true)
{
   //
}


That first line will assign 'true' to the variable 'started', so that if-block will always be entered. To check whether 'started' is true, change it to:

1  
2  
3  
4  
if(started == true)
{
   //
}

or
1  
2  
3  
4  
if(started)
{
   //
}

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline harry@dayfamilyweb

Junior Member





« Reply #21 - Posted 2006-04-27 11:45:18 »

it was reltated to my problem OWS was not intlized
Offline harry@dayfamilyweb

Junior Member





« Reply #22 - Posted 2006-04-27 11:59:21 »

got a nuther one Shocked Shocked Shocked Cry
init:
deps-jar:
compile:
run:
startting...
got output stream
Exception in thread "main" java.lang.NullPointerException
        at org.HSim.net.HSIP.server.ManServe.close(ManServe.java:145)
        at testofhsip.Main.main(Main.java:39)
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
public class Main {
   
    /** Creates a new instance of Main */
    public Main() {
    }
   
    /**
     * @param args the command line arguments
     */

    public static void main(String[] args) throws IOException {
        ManServe s = new ManServe();
        s.start();
        try {
            s.writeString("hi");
        } catch (IOException ex) {
            ex.printStackTrace();
        } catch (NotStarted ex) {
            ex.printStackTrace();
        }
        boolean exit = false;
        s.close(exit);
        if(exit == false) {
            System.out.println("will not close");
            s.close(exit);
        }
        // TODO code application logic here
    }
   
}

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
public void close(boolean closed) throws IOException {
            if(star == true) {
                h.close();
                OSW.close();
                run = false;
                closed = true;
            }  else {
                closed = false;
            }     
           
       
    }

star has to = true for h to have had time to intlize
Online Riven
« League of Dukes »

JGO Overlord


Medals: 605
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #23 - Posted 2006-04-27 12:09:35 »

Instead of posting each and every attempt you make and expect help, I'd suggest to look into what a NullPointerException means, from then on you can help yourself, which is much faster than asking for feedback on vague problems.


A NullPointerException occurs when trying to invoke a method / access a field from an reference pointing to Nothing:

Car car = new Car();
car.accelerate(); // works
car = null;
car.accelerate(); // throws NullPointerException, as "null".accelerate() is impossible, you can't accelerate 'nothing'.

With this knowledge, you should be able to fix your problems.

Have fun!

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline kevglass

JGO Kernel


Medals: 85
Projects: 25


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #24 - Posted 2006-04-27 20:38:29 »

Quote
Code:
OSW.write(infotosend);

that's line 131? Something in that line is null. It dosen't have to be OSW, it can be infotosend. As before, just test it for null before just before that line.

For reference, actually if that is line 131 and its throwing a null pointer exception it does infact have to be OSW that is null. infotosend isn't actually being dereferenced/used on that line so its perfectly valid for it to be null. If the null was infotosend then the NPE would be coming from deeper down the stack and not originating on that line.

Kev

Offline swpalmer

JGO Coder




Where's the Kaboom?


« Reply #25 - Posted 2006-04-27 21:11:43 »

I highly recommend following the same naming conventions as the rest of Java.  lowercase first letter for identifiers, Uppercase first letter on class names, packages all lowercase.  It just makes it easier for us to read if you are going to post every single time you hit the same error.

Offline harry@dayfamilyweb

Junior Member





« Reply #26 - Posted 2006-04-27 23:44:01 »

i tried trouble shotting it but it dose not make sense h(which is the only variable on that line) = serversocket.acept
Offline harry@dayfamilyweb

Junior Member





« Reply #27 - Posted 2006-04-28 00:27:22 »

i think it is becos the tread has not intlized it yet but how do i wait for it to do that (my computer has multy threading so i tried System.out.println(""); 200*
Online Riven
« League of Dukes »

JGO Overlord


Medals: 605
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #28 - Posted 2006-04-28 00:28:37 »

no, h is not the only variable... serversocket is also a variable, and it is null.

you're basicly doing:


Socket s = null.accept(); // null-pointer!

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline harry@dayfamilyweb

Junior Member





« Reply #29 - Posted 2006-04-28 00:35:45 »

the exception is in  org.HSim.net.HSIP.server.ManServe.close and it is the line h.close()
and serversocket = new ServerSocket(4444);
Pages: [1] 2
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (50 views)
2014-04-15 18:08:23

BurntPizza (46 views)
2014-04-15 03:46:01

UprightPath (62 views)
2014-04-14 17:39:50

UprightPath (44 views)
2014-04-14 17:35:47

Porlus (61 views)
2014-04-14 15:48:38

tom_mai78101 (84 views)
2014-04-10 04:04:31

BurntPizza (142 views)
2014-04-08 23:06:04

tom_mai78101 (242 views)
2014-04-05 13:34:39

trollwarrior1 (201 views)
2014-04-04 12:06:45

CJLetsGame (208 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!