-1

I have code in a .dll that a game in unity3d uses to connect to the server. and for some reason when the code in unity3d uses the code in the .dll it says object refernce no set to an instance of an object. Why is it throwing this error? im declaring everything before using it.

Edit* The reason im posting this is i cant find anything that would be causing this error nothing is null so please help and not complain iv tried searching but i cant find anything that shows what would be throwing the error.

code in .dll

    public void Start(string IP,int Port)
    {

        try
        {
            keyHandeler.create();
            TcpClient socket = new TcpClient();
            socket.Connect(IP, Port);
            st = socket.GetStream();
            Connection(true);
        }
        catch { Connection(false); }
    }

code in unity3d

    Client server = new Client();
    void Start () 
    {
         server.Start(IP, Port); // throws the object reference not set to an instance of an object
         // other stuff after this but it never reaches them
}
Shredder2500
  • 1,065
  • 2
  • 11
  • 26
  • possible duplicate of [What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net) – John Saunders Feb 24 '12 at 16:37
  • Did you try searching before asking? Even a little? – John Saunders Feb 24 '12 at 16:37
  • where does keyHandeler get created? maybe it's null – PeskyGnat Feb 24 '12 at 16:38
  • 2
    Where have you instantiated `server`? – Oded Feb 24 '12 at 16:39
  • i did try searching anything i found didnt work, and keyHandeler is a static class that keyHanderler.create() creates everything in the static class the server is instantiated in the class in unity – Shredder2500 Feb 24 '12 at 16:39
  • The first question for you should really have been: Have you debugged through your code? Have you identified the exact line throwing the exception? – Oded Feb 24 '12 at 16:44
  • server.start(IP,Port) is the line throwing the exception, and unity3d doesnt use the debuger on visual studios so there is not using the breaks. but it is server.start(IP,Port) throwing the error – Shredder2500 Feb 24 '12 at 16:45
  • That points to `server` being `null`. So, my first question stands - where are you instantiating `server`? – Oded Feb 24 '12 at 16:46
  • look at the code and my comment after your question, it is instantiated in the class before void Start(); – Shredder2500 Feb 24 '12 at 16:47

1 Answers1

1

set breakpoint on server.Start(IP, Port); and see if Server is null or not.

If its not null the Something is happening at keyHandeler.create(); or socket.Connect(IP, Port);.

Do you have access to DLL code? if yes they try to debug it by setting the break points and see if any object is null.

Scorpion
  • 4,495
  • 7
  • 39
  • 60
  • Unity3d dosnt use the visual studios debuger so i cant use the break points all i know is server.start(IP, Port) is throwing the error and that iv used the .dll before on a winForms and it worked fine – Shredder2500 Feb 24 '12 at 16:49
  • i just commented keyHandeler.create() out so its not that – Shredder2500 Feb 24 '12 at 16:52
  • I know its socket.Connect(IP,Port) because its throwing the code. good thing about unity is it highlights the line that is throwing the code. but im not sure why it is. it shouldnt be null. – Shredder2500 Feb 24 '12 at 16:58
  • you have try catch in your Start method so which is not throwing any exception, so I don't think something is wrong there. I think its not connecting because of IP and Port and its causing exception when you are using that connection somewhere. You can check that by putting `server.Start(IP, Port);` in try catch. – Scorpion Feb 24 '12 at 16:59
  • if you want to check your server object then write this code above the start call. `If(Server == null) throw new Exception("Server is null");` – Scorpion Feb 24 '12 at 17:01
  • Check the parameters IP and port and see if they are correct. These are the possible scenario there. – Scorpion Feb 24 '12 at 17:03
  • didnt trow a server is null, and also they are correct if they wernt they would cause the connection(false) in the try catch cause Tcpclient.connect would fail – Shredder2500 Feb 24 '12 at 17:04
  • Put this code for testing only; just after `socket.Connect(IP, Port);` `if(socket.Connected) {throw new Exception("Server is Connected"); else throw new Exception("Server not connected");}`. in this case you will see if its connecting or not. or even if its failing before that. – Scorpion Feb 24 '12 at 17:10
  • it means control is not getting there at all. Its breaking before that. – Scorpion Feb 24 '12 at 17:16
  • why is that? if server is not null then why is it not getting there? Also if the socket.connect(IP,Port) fails it throws an exception that the try catch picks up and calls the event that unity is listening for and will display cant connect to server witch is not happening – Shredder2500 Feb 24 '12 at 17:17
  • remove try catch first and then put that code after socket.connect line – Scorpion Feb 24 '12 at 17:18
  • when i removed the try catch it said server connected – Shredder2500 Feb 24 '12 at 17:20
  • now put this after `st = socket.GetStream();` see if its hitting the test code or not. its quite hard without debug. – Scorpion Feb 24 '12 at 17:21
  • i just commented out calling the event, that is the problem. any idea why? – Shredder2500 Feb 24 '12 at 17:24
  • YOur exception says that object is null... so you might need to drill down there.... please upvote or mark as answere if it does help. Regards – Scorpion Feb 24 '12 at 17:46
  • 1
    There is of course a debugger in MonoDevelop. Have a look at the manual, it would have saved a lot of time – Kay Feb 25 '12 at 11:00