Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Code just stops when string is initialised

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Code just stops when string is initialised

    I am supposed to have a system that takes data from a packet and does things with it. I had some code that was working fine, I made a few changes involving trying to get the instruction portion of the data out (the first 5 characters) and the code stops whenever the string "message" is initialised about halfway down. There are no errors or anything, the debugger just highlights the word "string" on that line and then just stops.

    I've been playing around with it, and it seems no matter what the situation whenver I initialise a second string in the program, the debugger highlights the last keyword in that line and then just stops. I'll post the current code below, then below that code from an earlier version which was working fine.

    Appreciate any help!

    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package reservationsystemserver;
    import java.io.*;
    import java.net.*;
    import java.util.*;
    /**
     *
     * @author Norman
     */
    public class serverActions implements Runnable {
     
        DatagramPacket packet = null;
        InetAddress returnIP = null;
        int returnPort = 0;
     
        public serverActions(DatagramPacket packet)
        {
            this.packet = packet;
            this.returnIP = packet.getAddress();
            this.returnPort = packet.getPort();
        }
     
        public void run()
        {
           DatagramPacket packet2;
           packet2 = packet;
           if (parseInstruction(packet).equals("LOGIN"))
           {
           login(packet2);
           }
     
     
        }
        private void login(DatagramPacket packet)
        {
            try
            {
     
            String instructionType, username;
            int loopBreak = 0;
     
            String message = new String(packet.getData(),0,packet.getLength());
            System.out.println(packet.getSocketAddress());
     
     
            StringTokenizer st = new StringTokenizer(message,".");
            instructionType = st.nextToken();
            username = st.nextToken();
            System.out.println(instructionType);
            System.out.println(username);
            System.out.println("Printed");
     
     
            File file = new File("C:\\Users\\Norman\\Documents\\NetBeansProjects\\ReservationSystemServer\\loggedInUsers.txt");
            while (loopBreak == 0)
            {
            if (file.canWrite())
                {
                  BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\Norman\\Documents\\NetBeansProjects\\ReservationSystemServer\\loggedInUsers.txt"));
                  String[] loggedInUsers = new String[1000];
                  int i = 0;
     
                  String value = br.readLine();
                  while (value != null)
                  {
                    loggedInUsers[i] = value;
                    i++;
                    value = br.readLine();
                  }
     
     
                  Writer output = null;
                  String eol = System.getProperty("line.separator");
                  output = new BufferedWriter(new FileWriter(file));
                  output.write(username);
                  output.write(" Has logged in from ");
                  output.write(returnIP.toString());
                  i = 0;
                  while (loggedInUsers[i] != null)
                  {
                      output.write(eol + loggedInUsers[i]);
                      i++;
                  }
                  output.close();
     
                  loopBreak = 1;
     
                }
            }
            }
            catch (IOException e){
                System.out.println(e.getMessage());
            }
            }
     
        private String parseInstruction(DatagramPacket packet)
        {
     
            String stringg = new String(packet.getData(),0,packet.getLength());
            StringTokenizer tf = new StringTokenizer(stringg,".");
            String instructionType = new String(tf.nextToken());
     
     
            return instructionType;
     
        }
        }

    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package reservationsystemserver;
    import java.io.*;
    import java.net.*;
    import java.util.*;
    /**
     *
     * @author Norman
     */
    public class serverActions implements Runnable {
     
        DatagramPacket packet = null;
        InetAddress returnIP = null;
        int returnPort = 0;
     
        public serverActions(DatagramPacket packet)
        {
            this.packet = packet;
            this.returnIP = packet.getAddress();
            this.returnPort = packet.getPort();
        }
     
        public void run()
        {
            try
            {
     
            String message, instructionType, username;
            int loopBreak = 0;
     
            message = new String(packet.getData(),0,packet.getLength());
            System.out.println(packet.getSocketAddress());
     
     
            StringTokenizer st = new StringTokenizer(message,".");
            instructionType = st.nextToken();
            username = st.nextToken();
            System.out.println(instructionType);
            System.out.println(username);
            System.out.println("Printed");
     
     
            File file = new File("C:\\Users\\Norman\\Documents\\NetBeansProjects\\ReservationSystemServer\\loggedInUsers.txt");
            while (loopBreak == 0)
            {
            if (file.canWrite())
                {
                  BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\Norman\\Documents\\NetBeansProjects\\ReservationSystemServer\\loggedInUsers.txt"));
                  String[] loggedInUsers = new String[1000];
                  int i = 0;
     
                  String value = br.readLine();
                  while (value != null)
                  {
                    loggedInUsers[i] = value;
                    i++;
                    value = br.readLine();
                  }
     
     
                  Writer output = null;
                  String eol = System.getProperty("line.separator");
                  output = new BufferedWriter(new FileWriter(file));
                  output.write(username);
                  output.write(" Has logged in from ");
                  output.write(returnIP.toString());
                  i = 0;
                  while (loggedInUsers[i] != null)
                  {
                      output.write(eol + loggedInUsers[i]);
                      i++;
                  }
                  output.close();
     
                  loopBreak = 1;
     
                }
            }
            }
            catch (IOException e){
            }
            }
     
     
        }


  2. #2
    Junior Member
    Join Date
    Apr 2012
    Location
    Missouri, United States
    Posts
    17
    Thanks
    4
    Thanked 2 Times in 2 Posts

    Default Re: Code just stops when string is initialised

    Are you using Eclipse or any compiler/debugger with Break Points? If so, make sure there is no Break Point on that the line with the String "message".

    If that is not the problem, perhaps it is related to the "packet2" variable in your run() method. It references the same DataGramPacket as "packet", and is not a clone or copy. Thus, if your parseInstruction() method modifies that "packet" variable in any way, it will affect "packet2". You could try making "packet2" a Deep Copy of "packet", by initializing "packet2" as a new DataGramPacket and setting its data to a copy of the data in "packet".

    Hope that helps!

  3. #3
    Junior Member
    Join Date
    Apr 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Code just stops when string is initialised

    Quote Originally Posted by Gigggas View Post
    Are you using Eclipse or any compiler/debugger with Break Points? If so, make sure there is no Break Point on that the line with the String "message".

    If that is not the problem, perhaps it is related to the "packet2" variable in your run() method. It references the same DataGramPacket as "packet", and is not a clone or copy. Thus, if your parseInstruction() method modifies that "packet" variable in any way, it will affect "packet2". You could try making "packet2" a Deep Copy of "packet", by initializing "packet2" as a new DataGramPacket and setting its data to a copy of the data in "packet".

    Hope that helps!
    Cheers for the reply!

    I'd read somewhere that everything in java was passed by value, not by reference! That's why I created the packet2 variable (very messy and unnecessary, only really there because I was trying absolutely anything!).

    I worked it out: I was using getData() twice and the second time it used getData() it cried. Still don't know why it wasn't throwing up an error though, cost me many hours of my life! When you use getData() does it change the packet? The debugger shows the packet as being the same all through the code, so I can't understand why I can only do that once.

  4. #4
    Junior Member
    Join Date
    Apr 2012
    Location
    Missouri, United States
    Posts
    17
    Thanks
    4
    Thanked 2 Times in 2 Posts

    Default Re: Code just stops when string is initialised

    Glad to see you got it fixed. I don't know much about packets, so I don't know why the getData() method would have any problem being called twice on the same object. It could be related to initializing two different Strings with that same data, but I really don't know.

Similar Threads

  1. Help with code for converting 4 digit string to integer
    By danielp1213 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 24th, 2011, 09:38 PM
  2. Replies: 2
    Last Post: October 22nd, 2011, 01:34 AM
  3. Ant Executes and Stops After a Certain Task
    By DanielPros in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 9th, 2011, 10:46 AM
  4. While loop stops.
    By MrFish in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 16th, 2010, 06:40 PM
  5. Check difference between no. of stops, calculate cost
    By JavaStudent23 in forum Java Theory & Questions
    Replies: 4
    Last Post: November 17th, 2009, 02:29 AM