Results 1 to 8 of 8
  1. #1
    ktden is offline Member
    Join Date
    Mar 2013
    Posts
    10
    Rep Power
    0

    Default problem with thread

    im doing a little exercise with threads;
    Java Code:
    import java.util.*;
    
    public class prueba1 implements Runnable{
        double time;
        public static int time1[];
        Thread pepe1;
        Thread pepe2;
        Thread pepe3;
        String a1;
        String a2;
        String a3;
        int a=500;
          
        public prueba1(){
            
            int time1[]=new int[3];
             for(int i=0;i<time1.length;i++){
             double ale1=(Math.random());
             double time=ale1*1000;
            time1[i]=(int) time;
             }
            a1="ejecutado1 en tiempo  "+time1[0];
            a2="ejecutado2 en tiempo  "+time1[1];
            a3="ejecutado3 en tiempo  "+time1[2];
           Thread pepe1=new Thread(this);
            pepe1.start();
        
    
        }
    
          public void run(){
                           try{
             pepe1.sleep(500);
             
              }catch (InterruptedException e) { }
                           System.out.println(a1);                       
                       try{
             pepe2.sleep(a);     
              }catch (InterruptedException e) { }
                       System.out.println(a2);
                       try{
             pepe3.sleep(time1[0]);        
              }catch (InterruptedException e) { }
                       System.out.println(a3);
        }  
    }
    public static void main(String[]args){
    prueba1 prueba1=new prueba1();
    
            }
    }
    pepe1 works ok
    pepe2 works ok
    but pepe3 gives an error on line 42
    why i cant pass the value of the array to the threat?

  2. #2
    FlyNn is offline Senior Member
    Join Date
    Feb 2010
    Posts
    128
    Rep Power
    0

    Default Re: problem with thread

    Whats the error?
    Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

  3. #3
    ktden is offline Member
    Join Date
    Mar 2013
    Posts
    10
    Rep Power
    0

    Default Re: problem with thread

    run:
    ejecutado1 en tiempo 829
    ejecutado2 en tiempo 483
    Exception in thread "Thread-0" java.lang.NullPointerException
    at prueba1.run(prueba1.java:50)
    at java.lang.Thread.run(Thread.java:722)
    BUILD SUCCESSFUL (total time: 1 second)

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: problem with thread

    Which is line 50? I'm guessing it's

    Java Code:
    pepe2.sleep(a);
    You don't initialise pepe2 anywhere, so you get a NullPointerException when this line of code is executed.

  5. #5
    ktden is offline Member
    Join Date
    Mar 2013
    Posts
    10
    Rep Power
    0

    Default Re: problem with thread

    sorry, i have comments at the begining that didnt post;
    run:
    ejecutado1 en tiempo 829
    ejecutado2 en tiempo 483
    Exception in thread "Thread-0" java.lang.NullPointerException
    at prueba1.run(prueba1.java:42)
    at java.lang.Thread.run(Thread.java:722)
    BUILD SUCCESSFUL (total time: 1 second)

    line 50 refers to line 42
    Java Code:
             pepe3.sleep(time1[0]);

    in pepe2 i give the value of a parameter called "a", and it works fine
    Java Code:
    pepe2.sleep(a);
    other suggestions?

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: problem with thread

    Correct the indentation in your code. As you posted it, it won't compile (the brackets don't match).

    sleep() is a static method, so it would be better to say Thread.sleep(500), Thread.sleep(a), etc instead of pepe1.sleep(500), pepe2.sleep(a), etc. Also classes should start with a capital letter: Prueba1.

    This is all to do with readability, but making the code readable will help with debugging.

    ---

    Java Code:
    pepe3.sleep(time1[0]);
    A NullPointerException occurs when you use something as if it had a value when it is really null. time1 is an array of ints so time1[0] is OK. And pepe3 is null - it is never initialised anywhere - but that's OK because sleep() is static. This only leaves time1.

    You can check the value of time1 by inserting a line of code:

    Java Code:
    System.out.println("time1=" + time1);
    pepe3.sleep(time1[0]);
    If it turns out that time1 is null then you should go back through your code to where you thought you had given time1 a non null value and figure out why that didn't happen.

  7. #7
    ktden is offline Member
    Join Date
    Mar 2013
    Posts
    10
    Rep Power
    0

    Default Re: problem with thread

    tx pbrockway2;

    the answer to System.out.println("time1=" + time1); is time1=null,
    in another forum i get an answer... to declare the array at the begining, so if in line 5 i write
    int time1[]=new int [3];
    it works
    Java Code:
    import java.util.*;
    
    public class prueba1 implements Runnable{
        double time;
        int time1[]=new int [3];
        Thread pepe1;
        Thread pepe2;
        Thread pepe3;
        String a1;
        String a2;
        String a3;
        int a=500;
    
        //int b;
          
        public prueba1(){
    
             for(int i=0;i<time1.length;i++){
             double ale1=(Math.random());
             double time=ale1*1000;
            time1[i]=(int) time;
             }
            a1="ejecutado1 en tiempo  "+time1[0];
            a2="ejecutado2 en tiempo  "+time1[1];
            a3="ejecutado3 en tiempo  "+time1[2];
           Thread pepe1=new Thread(this);
            pepe1.start();
            //int b=time1[0]
        }
    
          public void run(){
                           try{
             pepe1.sleep(500);
             
              }catch (InterruptedException e) { }
                           System.out.println(a1);                       
                       try{
             pepe2.sleep(a);     
              }catch (InterruptedException e) { }
                       System.out.println(a2);
                       try{
                           System.out.println("time1=" + time1); 
             pepe3.sleep(time1[0]);  
             
              }catch (InterruptedException e) { }
                       System.out.println(a3);
        }  
    }
    Java Code:
    public class main28{
    
    public static void main(String[]args){
    prueba1 prueba1=new prueba1();
    
            }
    }
    it works... but i dont understand why :-(

  8. #8
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: problem with thread

    i get an answer... to declare the array at the begining, so if in line 5 i write
    int time1[]=new int [3];
    it works

    ...

    it works... but i dont understand why :-(
    First of all note that you already had "int time1[]=new int [3];" as line 5 of your original code.

    The difference you made in in the public prueba1() constructor where you took out a similar line.

    In the original code you declared time1[] for a second time and when you do that you end up with a totally different array. (You are said to "mask" the first declaration). And that means that in the run() method time1 is still null. run() sees the first time1, not the one you initialised in the constructor.

Similar Threads

  1. thread problem
    By rowanabejjani in forum New To Java
    Replies: 3
    Last Post: 06-02-2011, 07:48 PM
  2. Thread Problem
    By zhein04 in forum Threads and Synchronization
    Replies: 2
    Last Post: 02-07-2011, 12:12 AM
  3. Thread problem.
    By Mekonom in forum New To Java
    Replies: 4
    Last Post: 03-16-2010, 04:19 PM
  4. thread problem
    By Harris68 in forum Networking
    Replies: 3
    Last Post: 03-03-2010, 07:53 PM
  5. Problem with a thread
    By dotabyss in forum Threads and Synchronization
    Replies: 3
    Last Post: 02-14-2010, 10:56 PM

Posting Permissions

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