harry@dayfamilyweb
|
 |
«
Posted
2006-04-25 07: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
|
|
|
|
Ask_Hjorth_Larsen
Junior Devvie  
Java games rock!
|
 |
«
Reply #1 - Posted
2006-04-25 16:14:57 » |
|
You call checkInput with a char[] which is null.
Rather fishy isn't it?
|
|
|
|
harry@dayfamilyweb
|
 |
«
Reply #2 - Posted
2006-04-25 21: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  i do not get why this exception is thrown
|
|
|
|
Games published by our own members! Check 'em out!
|
|
Kova
|
 |
«
Reply #3 - Posted
2006-04-25 22: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  they are really easy to fix 
|
|
|
|
harry@dayfamilyweb
|
 |
«
Reply #4 - Posted
2006-04-25 23: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);
|
|
|
|
harry@dayfamilyweb
|
 |
«
Reply #5 - Posted
2006-04-25 23: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
|
|
|
|
swpalmer
|
 |
«
Reply #6 - Posted
2006-04-26 00: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.
|
|
|
|
Kova
|
 |
«
Reply #7 - Posted
2006-04-26 00:56:37 » |
|
and please mark the line in code where nullexception is thrown
|
|
|
|
harry@dayfamilyweb
|
 |
«
Reply #8 - Posted
2006-04-26 02: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;
public class Main { public Main() { } public static void main(String[] args) throws IOException { HSIPrunner s = new HSIPrunner(); s.Start(); char[] r = "Hi".toCharArray(); s.write(r); } } |
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;
public class HSIPrunner extends Thread { private static final int PORT = 4444; private static Logger log = Logger.getLogger("java.net.HSIP"); HSIPProtocolHandler HSIP; 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() { 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; 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"); } [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); } |
|
|
|
|
kevglass
|
 |
«
Reply #9 - Posted
2006-04-26 02:58:34 » |
|
It appears "reader" is null.
Kev
|
|
|
|
Games published by our own members! Check 'em out!
|
|
swpalmer
|
 |
«
Reply #10 - Posted
2006-04-26 03:11:43 » |
|
yeah, when does processStreamIo get called?
|
|
|
|
kevglass
|
 |
«
Reply #11 - Posted
2006-04-26 03: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
|
|
|
|
swpalmer
|
 |
«
Reply #12 - Posted
2006-04-26 03:34:55 » |
|
Sigh... empty catch block... not even a comment in it... tsk. It's like you want the problems to be hidden.
|
|
|
|
kevglass
|
 |
«
Reply #13 - Posted
2006-04-26 03: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
|
|
|
|
swpalmer
|
 |
«
Reply #14 - Posted
2006-04-26 03: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.
|
|
|
|
|
CaptainJester
|
 |
«
Reply #16 - Posted
2006-04-26 11: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.
|
|
|
|
harry@dayfamilyweb
|
 |
«
Reply #17 - Posted
2006-04-27 00: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  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()); } } |
|
|
|
|
Kova
|
 |
«
Reply #18 - Posted
2006-04-27 08:02:55 » |
|
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.
|
|
|
|
|
Riven
|
 |
«
Reply #20 - Posted
2006-04-27 09: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
|
Hi, appreciate more people! Σ ♥ = ¾ Learn how to award medals... and work your way up the social rankings!
|
|
|
harry@dayfamilyweb
|
 |
«
Reply #21 - Posted
2006-04-27 09:45:18 » |
|
it was reltated to my problem OWS was not intlized
|
|
|
|
harry@dayfamilyweb
|
 |
«
Reply #22 - Posted
2006-04-27 09:59:21 » |
|
got a nuther one  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 { public Main() { } 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); } } } |
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
|
|
|
|
Riven
|
 |
«
Reply #23 - Posted
2006-04-27 10: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!
|
|
|
kevglass
|
 |
«
Reply #24 - Posted
2006-04-27 18:38:29 » |
|
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
|
|
|
|
swpalmer
|
 |
«
Reply #25 - Posted
2006-04-27 19: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.
|
|
|
|
harry@dayfamilyweb
|
 |
«
Reply #26 - Posted
2006-04-27 21:44:01 » |
|
i tried trouble shotting it but it dose not make sense h(which is the only variable on that line) = serversocket.acept
|
|
|
|
harry@dayfamilyweb
|
 |
«
Reply #27 - Posted
2006-04-27 22: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*
|
|
|
|
Riven
|
 |
«
Reply #28 - Posted
2006-04-27 22: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!
|
|
|
harry@dayfamilyweb
|
 |
«
Reply #29 - Posted
2006-04-27 22: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);
|
|
|
|
|