Exception in thread "main" java.lang.NullPointerException

Probably not related to your problem… but:

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:

if(started == true)
{
   //
}

or

if(started)
{
   //
}

it was reltated to my problem OWS was not intlized

got a nuther one :o :o :o :’(
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)


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
    }
    
}



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

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!

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

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.

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

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*

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!

the exception is in org.HSim.net.HSIP.server.ManServe.close and it is the line h.close()
and serversocket = new ServerSocket(4444);

Well, then h == null. Why don’t you just System.out.println the variables before the exception is thrown as everyone as suggested?

And when you post code you still don’t specify which lines the exception propagate through.

And you still call


char[] C = null;
checkInput(IS , C);

Why not just call checkInput(IS, null) ? It looks like if C isn’t supposed to be null since you take the trouble to declare it!

check inout sets the value of C ;D ;D ;D ;D ;D

Its like a hideous car accident you see on the way home from work, its horrible and you feel sorry for the people involved but you can’t help but stare. - sorry, wasn’t adding anything

You can’t set C inside the function, it doesn’t exist, you’d only be setting the local scope pointer in the function. To do what you’re aiming for you’d have declared the array, pass that in and set the contents.

Kev

??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ::slight_smile: :’(

Sigh, I pointed it out in the very first post of this thread, yet it seems to have been forgotten. Anyway, C is a reference to null. When you pass that to the function it will NOT be able to change your reference. It can manipulate the OBJECT which your reference points to, for example it could put values into an array. However it cannot put values into null.

And while the method could create a new array and assigning a reference to it and put things into that, C still points to null.

You need to get hold of some more books.

would it work if c = new char[20] ???

it works now :o :o ;D :smiley: :wink: :slight_smile: :wink: :smiley: ;D ;D ;D :o :o :o
i turn it into a thread ;D ;D ;D ;D ;D ;D ;D ;D
( ), adra kadabra
/|/
/
init:
deps-jar:
compile:
run:
intlizing
startting…
got output stream
starting
yield
BUILD SUCCESSFUL (total time: 4 seconds)


package testofhsip;

import java.io.IOException;
import org.HSim.net.HSIP.NotStarted;
import org.HSim.net.HSIP.server.ManServe;
/**
 *
 * @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 {
        
        A a = new A();
        a.start();
        
        // TODO code application logic here
    }
    public static class A extends Thread {
        ManServe s = new ManServe();
        public void start() {
            System.out.println("intlizing");
            try {
                this.sleep(100);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
            s.start();
            this.yield();            
            System.out.println("starting");
            System.out.println("yield");
            try {
                this.sleep(1100);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
            this.yield();            
                      
        }
        public void run() {
            System.out.println("running;-)");
            try {
                System.out.println("................................");
                System.out.println("watting for thred....");
                System.out.println("................................");
                System.out.println("preparing to write!....");    
                this.yield();
                try {this.sleep(1100);
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
                
                System.out.println("writing!!!....");
                System.out.println("hi");
                System.out.println("................................");
                s.writeString("hi");
                this.yield();
                try {this.sleep(1100);
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                }

                System.out.println("writing!!!....");
                System.out.println("................................");
                System.out.println("writting :_)");            
                System.out.println("yo");
                s.writeString("yo");
                this.yield();
                try {this.sleep(1100);
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                }

                System.out.println("writing!!!....");
                System.out.println("................................");
                System.out.println("cool");
                s.writeString("cool");
                this.yield();
                try {this.sleep(1100);
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                }

                System.out.println("writing!!!....");
                System.out.println("................................");
                System.out.println("bye");
                s.writeString("bye");
                this.yield();
                try {this.sleep(1100);
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                }

                System.out.println("preparing to close");
                System.out.println("wating");
                System.out.println("................................");
            } catch (IOException ex) {
                ex.printStackTrace();
            } catch (NotStarted ex) {
                ex.printStackTrace();
            }
            boolean exit = false;
            try {
                s.close(exit);
                this.yield();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            while(exit == false) {
                System.out.println("will not close");
                System.out.println("wil not close");
                System.out.println("clossing");
                try {
                    s.close(exit);
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
                System.out.println("tryed");
            }
            if(exit == true) {
                System.out.println("closed");
            }
        }
    }
    
}


Do you understand that in code like this:

public void close(boolean closed) throws IOException {
            if(star == true) {
                h.close();
                OSW.close();
                run = false;
                closed = true;
            }  else {
                closed = false;
            }     
           
       
    }

the assignment to ‘closed’ does absolutely nothing? ‘closed’ is passed BY VALUE, like everything in Java. All that code does is change the value of the LOCAL variable ‘closed’, the actual variable that you passed when you called the method remains unaffected.

In particular, this block of your latest code

            boolean exit = false;
            try {
                s.close(exit);
                this.yield();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            while(exit == false) {
                System.out.println("will not close");
                System.out.println("wil not close");
                System.out.println("clossing");
                try {
                    s.close(exit);
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
                System.out.println("tryed");
            }
            if(exit == true) {
                System.out.println("closed");
            }

has an infinite while-loop because ‘exit’ = false and it can never change. Since it is a local variable it can’t be touched outside that thread and nothing in that thread changes it after it has been initialized.

I don’t intend to sound mean, but your code shows a basic lack of understanding of the Java language in general. You should probably work on that a bit before you tackle writing a game. Game programming is usually not beginner-level coding, unless you start with some very simple games, like Tic-Tac-Toe.
Have you gone through the Java tutorials a java.sun.com? They are a good place to get familiar with the language.

You should probably post in Newless Clubies unless you are really having a problem with networking as opposed to just general coding issues.

.

not intierly

incorect whith arays ;D becouse any aray can be refrenced from any class in the same JVM becous they are stored in the stack(well this is my gues but it works anyway ;D)my exampl is


char[] c = new char[20];

  
 String s = "s";
 s.getChars(0 , 20 , c , 0);


that should work ;D
s.getChars(0 , 20 , c , 0); is a void not an char[]
you shoould read the java docs ;D ;D ;D :stuck_out_tongue: :stuck_out_tongue: ::slight_smile: 8) 8) :o :slight_smile: :wink: :smiley:

What the heck are you talking about?

Of course you can manipulate an array from a method by passing a reference to that method, that’s completely basic! Arrays can be referenced in exactly the same way as any other object can. And objects are stored on the heap, whereas references as such may be stored on the stack, though this is quite irrelevant to the discussion.

Allow me to reiterate the point with my previous post, namely that a null reference cannot be changed from inside a method to which it is passed. This is because passing a reference actually creates a copy of that reference, thus you cannot manipulate the original reference.