Results 1 to 9 of 9
  1. #1
    _rapt0r_ is offline Member
    Join Date
    Nov 2011
    Posts
    24
    Rep Power
    0

    Default Help with ArrayList

    hello people, I am new to the world of java and I'm trying to do a p2p game in java, but'm here with a small problem .. UDPRecebe my class receives a message with the name of online users, and I wanted to pass the value of this message in a arraylist to a method in another class called listaUtilizadores. The problem is that the result comes null. Anyone can help me?

    Java Code:
    public class UDPRecebe implements Runnable {
    
        static final int PORTO = 3110;
        static final int MAX_TAMANHO_MSG = 1024;
        //public static HashMap<String , String[]> info = new HashMap<>();
       // ArrayList<String> nickList = new ArrayList<>();
        public String[] nickList;
       Jogador joga;
        public UDPRecebe(Jogador joga) {
            this.joga = joga;
            
            
        }
       
        
    
        public void run() {
            
            try {
                System.out.println("Estou à escuta na porta " + PORTO);
                //criar um buffer de bytes para colocar o conteudo do datagrama
                byte[] buffer = new byte[MAX_TAMANHO_MSG];
                String msg;
    
                // Criar um socket UDP para receber datagramas no porto indicado
                DatagramSocket recebe = new DatagramSocket(PORTO);
    
                while (true) {
                    //Criar um objecto DatagramPacket chamado packet para receber uma mensagem
                    DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
    
                    // Esperar que chegue algum datagrama
                    recebe.receive(packet);
    
                    //Apresentar ao utilizador de que endereco e porto remotos chegou este datagrama
                    System.out.println("O datagrama chegou do seguinte par endereço : porto " + packet.getSocketAddress());
    
                    // Converter o conteudo do datagrama para uma string e apresenta-lo
                    msg = new String(buffer, 0, packet.getLength());
                    String ip = packet.getAddress().getHostAddress();
                    processar(ip, msg);
                   
    
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    
        public void processar(String ip, String msg) {
           
            String[] detalhes = msg.split(" ");          
            String [] nick = detalhes[1].split(";");
            String nome = detalhes[0];
            nickList = nick;
           
             
            String estado = detalhes[2];
            //Em caso de um pedido PLAYER_Info gera uma resposta PLAYER_INFO_REPLY.
            switch (detalhes[0]) {
                case "PLAYER_INFO":
                    try {
                        
                        UDPEnvia.enviaMsg(ip, "PLAYER_INFO_REPLY "+joga.getName()+";"+joga.getMyState());
                      // info.put(ip, new String[](nome, estado));
                      
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                
              
            }
           
            }
    
        }
    and needed to pass the value of ip, name and status for this class

    Java Code:
    public class ServidorPresencas {
    
        private String[] utilizadoresOnline;
        Jogador joga;
    
        UDPRecebe recebe = new UDPRecebe(joga);
        public String[] nickList = recebe.nickList;
    
        public ServidorPresencas(Jogador joga) {
            this.joga = joga;
            
            
        }
    
        public void temporizador() {
    
            Timer timer = new Timer();
            int startingTime = 0;
            int delayTime = 30000;
            timer.schedule(new TimerTask() {
    
                @Override
                public void run() {
                    ligarServidor();
                    getInfo();
                }
            }, startingTime, delayTime);
    
        }
    
        private void ligarServidor() {
            try {
                URL servPresencas = new URL("http://193.137.8.61/heartbeat/heartbeat.svc/show?ip=" + InetAddress.getLocalHost().getHostAddress());
                URLConnection yc = servPresencas.openConnection();
                try (BufferedReader in = new BufferedReader(
                                new InputStreamReader(
                                yc.getInputStream()))) {
                    String inputLine;
    
                    while ((inputLine = in.readLine()) != null) {
                        utilizadoresOnline = inputLine.split(";");
                    }
    
                    listarUtilizadores();
                }
            } catch (MalformedURLException ex) {
                System.out.println(ex);
            } catch (IOException ex) {
                System.out.println(ex);
            }
        }
    
        private void listarUtilizadores() {
            for (int i = 0; i < utilizadoresOnline.length; i++) {
                System.out.println(nickList);
                System.out.println("o ip: " + utilizadoresOnline[i] +"");
            }
        }
    
        public void getInfo() {
            // String nome = joga.getName();
            for (String str : utilizadoresOnline) {
                try {
                    UDPEnvia.enviaMsg(str, "PLAYER_INFO " + joga.getName() +"; livre");
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        
        try {
                         synchronized(this){
                         
                        
                             wait(30000);
                        
                        }
                    } catch (InterruptedException ex) {
                        Logger.getLogger(ServidorPresencas.class.getName()).log(Level.SEVERE, null, ex);
                    }
    }
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default Re: Help with ArrayList

    the result comes null.
    If you get an exception, please post the error message that shows where the exception occurs.
    Or you if have other debug output that shows the problem, please post it.

    Your code does not have a method: listaUtilizadores

  3. #3
    _rapt0r_ is offline Member
    Join Date
    Nov 2011
    Posts
    24
    Rep Power
    0

    Default Re: Help with ArrayList

    Quote Originally Posted by Norm View Post
    If you get an exception, please post the error message that shows where the exception occurs.
    Or you if have other debug output that shows the problem, please post it.

    Your code does not have a method: listaUtilizadores
    the method called listarUtilizadores.
    Gives me no error, but the variable nickList receives a null value and i want one String[].

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default Re: Help with ArrayList

    a method in another class called listaUtilizadores.
    the method called listarUtilizadores.
    Are there two methods or is the first one misspelled? I don't speak Spanish(??) or I might have noticed.

    You need to make sure the nickList variable has a value and is not null.
    Try debugging your code by adding a println that shows the value of nickList at every place in the code where the code changes the value of nickList. The printout will help you see why nickList has a null value and where it is coming from.

  5. #5
    _rapt0r_ is offline Member
    Join Date
    Nov 2011
    Posts
    24
    Rep Power
    0

    Default Re: Help with ArrayList

    excuse my portuguese / english. are two classes, one class receives a message via UDP and the method processar (process) cuts this message in 3, Player_info, name and state. I want to pass this information to another class called servidorPresenças (Presence Server) for the method listarUtilizadores (User's list)

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default Re: Help with ArrayList

    You need to make sure the nickList variable has a value and is not null. Have you tried the followng?
    Try debugging your code by adding a println that shows the value of nickList at every place in the code where the code changes the value of nickList. The printout will help you see why nickList has a null value and where it is coming from.

    What is the value of nickList when this statement is executed?
    public String[] nickList = recebe.nickList;

    Print it out in the ServidorPresencas class's constructor to see what it is.
    Last edited by Norm; 11-18-2011 at 05:42 PM.

  7. #7
    _rapt0r_ is offline Member
    Join Date
    Nov 2011
    Posts
    24
    Rep Power
    0

    Default Re: Help with ArrayList

    Java Code:
      private void processar(String ip, String msg) {
           
            String[] detalhes = msg.split(" ");          
            String [] nick = detalhes[1].split(";");
            String name = detalhes[0];
            nickList.add(nick);
           
           
             
            String state = detalhes[2];
            //Em caso de um pedido PLAYER_Info gera uma resposta PLAYER_INFO_REPLY.
            switch (detalhes[0]) {
                case "PLAYER_INFO":
                    try {
                        System.out.println("o seu"+joga.getMyState());
                        UDPEnvia.enviaMsg(ip, "PLAYER_INFO_REPLY "+joga.getName()+";"+joga.getMyState());
                         info.put(ip, new String[](name, state));
                      //info.put(ip, nick);
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                
               
            }
           
            }
    Give me a error in line 17 "array dimension missing, ; expected, not a statement. What´s wrong??

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default Re: Help with ArrayList

    Check that you are using the proper delimiters for the String array: {} vs ()

  9. #9
    _rapt0r_ is offline Member
    Join Date
    Nov 2011
    Posts
    24
    Rep Power
    0

    Default Re: Help with ArrayList

    Yes it is, put instead of {} () and resulted, thank you

Similar Threads

  1. Replies: 12
    Last Post: 07-07-2011, 08:49 PM
  2. copying contents of an ArrayList to another ArrayList
    By ankit1801 in forum New To Java
    Replies: 8
    Last Post: 03-27-2011, 06:07 AM
  3. sorting arraylist based on another arraylist
    By busdude in forum New To Java
    Replies: 4
    Last Post: 02-07-2011, 11:48 AM
  4. Replies: 7
    Last Post: 11-22-2010, 01:26 PM
  5. Replies: 2
    Last Post: 04-21-2008, 11:43 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •