locked
System.NullReferenceException was unhandled RRS feed

  • Question

  • Hi,
    hope somone can solve my problem.

    Code:
    using System.Net.Sockets;
    using System.Net;

    TcpListener[] listener;

    int countListener = 0;

     

    public void newConnection(IPAddress IP, int port){

          listener[countListener] = new TcpListener(IP, port);  // This line throws the NullReferenceException
          countListener++;

    }

    IP and port are set and count Listener is 0.

    Thursday, May 12, 2011 12:36 AM

Answers

  • Hello friend

    I found that you forgot to initialize the TcpListener array. Since it is not initialized it throws NullReferenceException.

    TcpListener[] listener = new TcpListener

    [10];

     

    int

    countListener = 0;

     

    public void newConnection(IPAddress IP, int

    port)

    {

    listener[countListener] =

    new TcpListener(IP, port);

    // This line throws the NullReferenceException

    countListener++;

    }

    hope this will help you.

    • Proposed as answer by Louis.fr Thursday, May 12, 2011 10:15 AM
    • Marked as answer by 060107 Thursday, May 12, 2011 7:20 PM
    Thursday, May 12, 2011 6:42 AM
  • TcpListener[]???

    Should be TcpListener


    Can't you listen to several ports?

    Yes, more than one can be listened to.  Jobin was more correct.

    The OP might want a LIST<TcpListener> instead of the static TcpListener[]???

    so


    List<> TcpListener = new List<TcpListener>();
    

     

    countrystyle

     

     

    • Marked as answer by 060107 Thursday, May 12, 2011 7:12 PM
    Thursday, May 12, 2011 1:35 PM

All replies

  • TcpListener[]???

    Should be TcpListener

     

     

    using System.Net.Sockets;
    using System.Net;
    
    TcpListener listener;
    
    int countListener = 0;
    
     
    
    public void newConnection(IPAddress IP, int port){
    
      listener = new TcpListener(IP, port); 
      countListener++;
    
    }
    

     

    countryStyle

    Thursday, May 12, 2011 3:20 AM
  • Hello friend

    I found that you forgot to initialize the TcpListener array. Since it is not initialized it throws NullReferenceException.

    TcpListener[] listener = new TcpListener

    [10];

     

    int

    countListener = 0;

     

    public void newConnection(IPAddress IP, int

    port)

    {

    listener[countListener] =

    new TcpListener(IP, port);

    // This line throws the NullReferenceException

    countListener++;

    }

    hope this will help you.

    • Proposed as answer by Louis.fr Thursday, May 12, 2011 10:15 AM
    • Marked as answer by 060107 Thursday, May 12, 2011 7:20 PM
    Thursday, May 12, 2011 6:42 AM
  • TcpListener[]???

    Should be TcpListener


    Can't you listen to several ports?
    Thursday, May 12, 2011 10:15 AM
  • TcpListener[]???

    Should be TcpListener


    Can't you listen to several ports?

    Yes, more than one can be listened to.  Jobin was more correct.

    The OP might want a LIST<TcpListener> instead of the static TcpListener[]???

    so


    List<> TcpListener = new List<TcpListener>();
    

     

    countrystyle

     

     

    • Marked as answer by 060107 Thursday, May 12, 2011 7:12 PM
    Thursday, May 12, 2011 1:35 PM
  • Thank you.

    Forgot to use a list insted of array when i changed from listener1, listener2, listener3, ...

     

    @Jobin John

    This would work to but i want a non static version.

    Compiler warned me to initialize but i thought i could initialize at runtime.

    Thursday, May 12, 2011 7:20 PM