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 13 of 13

Thread: MuliThreading Unable to get outputs but many NPES.....

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default MuliThreading Unable to get outputs but many NPES.....

    I have been trying for days and nights , no compile error , but alot of NPEs error .......

    Please help !!! I am working very hard to solve this assignment.

    Thank you so so much in advance.

    This is my Test program

    ----------
    import java.util.*;
     
    public class Test
    {
     
    public static void main (String[]args)
    {
    int totalShares = 0 ;
    double totalCost = 0.0;
    double totalProfitLoss = 0.0; 
    int rand1 = 0;
    double rand2 = 0.0;
    Stock stock = new Stock("FCS",totalShares,totalCost,totalProfitLoss);
     
    BuyThread bt1 = new BuyThread (rand1 , rand2);
    BuyThread bt2 = new BuyThread (rand1 , rand2);
    SellThread st1 = new SellThread (rand1 , rand2);
    SellThread st2 = new SellThread (rand1 , rand2);
    bt1.start();
    bt2.start();
    st1.start();
    st2.start();
     
    try
    {
    System.out.println ("Tracking of stock:  " +stock.name);
    bt1.join();
    bt2.join();
    st1.join();
    st2.join();
     
    System.out.println ("Total shares now " +stock.totalShares + " at total cost " +stock.totalCost );
    System.out.println ("At$ 36.00 per share, profit is " + stock.getProfit(36.00));
    }
    catch (InterruptedException e)
    {}
    }
    }

    This is my Stock Class

    ----------
    import java.util.*;
    public class Stock
    {
    String name;
    int totalShares;
    double totalCost; 
    double totalProfitLoss;
     
     
        public Stock(String name, int totalShares, double totalCost,double totalProfitLoss )
        {
            this.name = name;
            this.totalShares = totalShares;
            this.totalCost = totalCost;
            this.totalProfitLoss = totalProfitLoss;
        } 
     
        public String getName()
        {
            return name;
        }
        public double getTotalShares () 
        {
            totalShares++;
            return totalShares;
        }
        public double getTotalCost () 
        {
            totalCost++;
            return totalCost;
        }
        public double getTotalProfitLoss() 
        {
     
            return totalProfitLoss;
        }
     
        public void buy(int shares, double pricePerShare)
        {
            totalShares += shares;
            totalCost += shares * pricePerShare;
        }
     
        public boolean sell(int shares, double pricePerShare) 
        {
          double sellCost = shares * pricePerShare;
     
          if (shares <= totalShares && sellCost <= totalCost){
              totalShares -= shares;
              totalCost -= sellCost;
              return true;
            }
            else {
                return false;
            }
        }
     
        public double getProfit (double currentPrice)
        {
            totalProfitLoss = currentPrice * totalShares;
            return totalProfitLoss - totalCost;
        }
    }

    This is my BuyThread program


    ----------
    import java.util.*;
    public class BuyThread extends Thread
    {
    private int share ;
    private double cost;
    private Stock stock;
     
    public BuyThread (int share,double cost)
    {
    this.share = share ;
    this.cost = cost ;
    }
     
    public void run()
    {
    for (int j = 0 ; j < 5; j++)
    {
    Random randShare = new Random ();
    int rand1 = randShare.nextInt (50) + 5 ;
    double rand2 = (int) ((Math.random() * 10 + 32)* 100.0)/100.0 ;
    stock.buy (rand1 , rand2);
    System.out.println (rand1+" shares has been brought at $"+rand2) ;
    try
    { sleep (100);
    }
    catch (InterruptedException e)
    {}
    }
    }
    }

    This is my SellThread

    ----------
    import java.util.*;
    public class SellThread extends Thread
    {
    private int share ;
    private double cost;
    private Stock stock;
     
    public SellThread (int share,double cost)
    {
    this.share = share ;
    this.cost = cost ;
    //this.stock = stock;
    }
     
    public void run()
    {
    for (int j = 0 ; j < 2; j++)
    {
    Random randShare = new Random ();
    int rand1 = randShare.nextInt (20) + 20 ;
    double rand2 = (int) ((Math.random() * 32 + 23)* 100.0)/100.0 ;
    stock.sell (rand1 , rand2);
    System.out.println (rand1+"shares has been brought at $"+rand2) ;
     
    try
    {
    Thread.sleep(60);
    }
    catch (InterruptedException e)
    {}
    }
    }
    }


    I am expecting a output like this (Which I have error running:

    Tracking of Stock : FCS
    8 shares has been brought at $37.61
    Total shares now 8 at total cost $300.88
    33 shares has been brought at $36.31
    Total shares now 41 at total cost $1499.11
    17 shares has been sold at $42.67
    Total shares now 24 at total cost $773.72
    19 shares has been sold at $32.31
    Total shares now 5 at total cost $159.83
    31 shares has been brought at $33.85
    Total shares now 36 at total cost $1209.18
    28 shares has been brought at $36.37
    Total shares now 64 at total cost $2227.54
    20 shares has been brought at $35.49
    Total shares now 84 at total cost $2937.34
    At $36.00 per share, profit is $86.66

    --- Update ---

    No experts around at this hour?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,105
    Thanks
    48
    Thanked 2,153 Times in 2,126 Posts

    Default Re: MuliThreading Unable to get outputs but many NPES.....

    alot of NPEs error
    Please copy the full text of the error message and paste it here. It has important info about the error.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    tanchiwoo (February 14th, 2014)

  4. #3
    Junior Member
    Join Date
    Feb 2014
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: MuliThreading Unable to get outputs but many NPES.....

    Good Day , Sir ! Your help is greatly appriciated .

    This is th output i got



    Tracking of stock: FCS
    Total shares now 0 at total cost 0.0
    At$ 36.00 per share, profit is 0.0

    ----------------------------------------------------------------

    Exception in thread "Thread-1" Exception in thread "Thread-2" java.lang.NullPointerException
    at BuyThread.run(BuyThread.java:21)
    java.lang.NullPointerException
    at BuyThread.run(BuyThread.java:21)
    Exception in thread "Thread-4" java.lang.NullPointerException
    at SellThread.run(SellThread.java:22)
    Exception in thread "Thread-3" java.lang.NullPointerException
    at SellThread.run(SellThread.java:22)

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,105
    Thanks
    48
    Thanked 2,153 Times in 2,126 Posts

    Default Re: MuliThreading Unable to get outputs but many NPES.....

    Have you looked at the source code lines referenced in the error messages and found the variables that have null values?
    When you find the variables with the null values, then back track in the code to see why those variables do not have valid values.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #5
    Junior Member
    Join Date
    Feb 2014
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: MuliThreading Unable to get outputs but many NPES.....

    Hi Sir, I dont know how to read....... Can you tell me which line have problems? Or which variables I point wrongly ?

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,105
    Thanks
    48
    Thanked 2,153 Times in 2,126 Posts

    Default Re: MuliThreading Unable to get outputs but many NPES.....

    Exception in thread "Thread-1" Exception in thread "Thread-2" java.lang.NullPointerException
    at BuyThread.run(BuyThread.java:21)
    java.lang.NullPointerException
    at BuyThread.run(BuyThread.java:21)
    Exception in thread "Thread-4" java.lang.NullPointerException
    at SellThread.run(SellThread.java:22)
    Exception in thread "Thread-3" java.lang.NullPointerException
    at SellThread.run(SellThread.java:22)
    The above shows the NPE happened at line 21 in BuyThread and line 22 in SellThread. Look at those lines and find the variables that have null values. If you can't see which variables are null, add a println() statement just before those statements that prints out the values of the variables used on those lines.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #7
    Junior Member
    Join Date
    Feb 2014
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: MuliThreading Unable to get outputs but many NPES.....

    Hi Sir, I wrote a Pool class to see what output I got.
    Looks good
    " 38 shares has been brought at $ 53.87
    26 shares has been brought at $ 43.04 "

    Now my problem is how do i call the buy method or sell method in my Stock Class ?
    I assumed I that I wrote the wrong thing here
    'stock.buy (rand1 , rand2);
    System.out.println (rand1+" shares has been brought at $"+rand2) ;'


    import java.util.*;
    public class Pool
    {
    private int rand1 ;
    private double rand2;
    private Stock stock;
     
    public Pool (int rand1,double rand2)
    {
    this.rand1 = rand1 ;
    this.rand2 = rand2 ;
    //this.stock = stock;
    }
     
    public static void main (String[]args)
    {
    for (int j = 0 ; j < 2; j++)
    {
    Random randShare = new Random ();
    int rand1 = randShare.nextInt (20) + 20 ;
    double rand2 = (int) ((Math.random() * 32 + 23)* 100.0)/100.0 ;
    System.out.println (rand1+" shares has been brought at $ "+rand2) ;
    //stock.sell (rand1 , rand2);
    //System.out.println (rand1+"shares has been brought at $"+rand2) ;
     
    try
    {
    Thread.sleep(60);
    }
    catch (InterruptedException e)
    {}
    }
    }
    }

  9. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,105
    Thanks
    48
    Thanked 2,153 Times in 2,126 Posts

    Default Re: MuliThreading Unable to get outputs but many NPES.....

    how do i call the buy method or sell method in my Stock Class ?
    That looks like the way to call a method in the Stock class:
    stock.buy (rand1 , rand2);
    If you don't understand my answer, don't ignore it, ask a question.

  10. #9
    Junior Member
    Join Date
    Feb 2014
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: MuliThreading Unable to get outputs but many NPES.....

    But Still , i dont know why it show me NPEs on line 21 ...... I thought I did the correct way

  11. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,105
    Thanks
    48
    Thanked 2,153 Times in 2,126 Posts

    Default Re: MuliThreading Unable to get outputs but many NPES.....

    it show me NPEs on line 21
    What variable has a null value on line 21?
    If you don't understand my answer, don't ignore it, ask a question.

  12. #11
    Junior Member
    Join Date
    Feb 2014
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: MuliThreading Unable to get outputs but many NPES.....

    Ok , now i got my output but , everything doesnt seem to be in order. I tried for days and nights. Now I understand more about Multi Threading. How do i get the output as what i expected ? Can someone advise me which part did I do wrongly ?

    This is my Test program

    ----------
        import java.util.*;
     
        public class Test
        {
     
        public static void main (String[]args)
        {
        int totalShares = 0;
        double totalCost = 0.0;
        double totalProfitLoss = 0.0; 
        int rand1 = 0;
        double rand2 = 0.0;
        Stock stock = new Stock("FCS",totalShares,totalCost,totalProfitLoss);
     
        System.out.println ("Tracking of stock:  " +stock.name);
        BuyThread bt = new BuyThread (rand1 , rand2 , stock);
        SellThread st = new SellThread (rand1 , rand2 , stock);
        bt.start();
        st.start();
     
        try
        {
        bt.join();
        st.join();
     
     
        System.out.println ("At$ 36.00 per share, profit is " + stock.getProfit(36.00));
        }
        catch (InterruptedException e)
        {}
        }
        }

    This is my Stock Class

    ----------
        import java.util.*;
        public class Stock
        {
        String name;
        int totalShares;
        double totalCost; 
        double totalProfitLoss;
     
     
        public Stock(String name, int totalShares, double totalCost,double totalProfitLoss )
        {
            this.name = name;
            this.totalShares = totalShares;
            this.totalCost = totalCost;
            this.totalProfitLoss = totalProfitLoss;
        } 
     
        public String getName()
        {
            return name;
        }
        public double getTotalShares () 
        {
            //totalShares++;
            return totalShares;
        }
        public double getTotalCost () 
        {
           // totalCost++;
            return totalCost;
        }
        public double getTotalProfitLoss() 
        {
     
            return totalProfitLoss;
        }
     
     
     
        public void buy(int shares, double pricePerShare)
        {
            totalShares += shares;
            totalCost += shares * pricePerShare;
            System.out.println ("Total shares now at " +totalShares+ " at total cost $ " +totalCost);
     
        }
     
        public boolean sell(int shares, double pricePerShare) 
        {
          double sellCost = shares * pricePerShare;
     
     
          if (shares >= totalShares || sellCost >= totalCost){
     
              return false;
     
            }
            else {
              totalShares -= shares;
              totalCost -= sellCost;
     
              System.out.println ("Total shares now at " +totalShares+ " at total cost $ "+totalCost);   
     
                return true;
     
            }
     
        }
     
        public double getProfit (double currentPrice)
        {
            totalProfitLoss = currentPrice * totalShares;
            return totalProfitLoss - totalCost;
        }
     
        }

    This is my BuyThread program


    ----------
        import java.util.*;
        public class BuyThread extends Thread
        {
        private int rand1 ;
        private double rand2;
        private Stock stock;
     
        public BuyThread (int rand1,double rand2,Stock stock)
        {
        this.rand1 = rand1 ;
        this.rand2 = rand2 ;
        this.stock = stock;
        }
     
        public void run()
        {
        for (int j = 0 ; j < 2; j++)
        {
        Random randShare = new Random ();
        int rand1 = randShare.nextInt (50) + 5 ;
        double rand2 = (int) ((Math.random() * 10 + 32)* 100.0)/100.0 ;
     
        stock.buy (rand1, rand2);
        System.out.println (rand1+ " shares has been brought at $"+rand2) ;
     
     
        try
        { sleep (50);
        }
        catch (InterruptedException e)
        {}
        }
        }
        }

    This is my SellThread

    ----------
        import java.util.*;
        public class SellThread extends Thread
        {
        private int rand1 ;
        private double rand2;
        private Stock stock;
     
        public SellThread (int rand1,double rand2,Stock stock)
        {
        this.rand1 = rand1 ;
        this.rand2 = rand2 ;
        this.stock = stock;
        }
     
        public void run()
        {
        for (int j = 0 ; j < 2; j++)
        {
        Random randShare = new Random ();
        int rand1 = randShare.nextInt (20) + 20 ;
        double rand2 = (int) ((Math.random() * 32 + 23)* 100.0)/100.0 ;
     
        stock.sell (rand1 , rand2);
        System.out.println (rand1+" shares has been sold at $"+rand2) ;
     
     
        try
        {
        Thread.sleep(30);
        }
        catch (InterruptedException e)
        {}
        }
        }
        }


    I got this output

    Tracking of stock: FCS
    Sell Total shares now at 13 at total cost $ 381.0999999999997
    Buy Total shares now at 50 at total cost $ 1664.9999999999998
    37 shares has been sold at $34.7
    50 shares has been brought at $33.3
    32 shares has been sold at $51.97
    Buy Total shares now at 18 at total cost $ 555.1499999999996
    5 shares has been brought at $34.81
    At$ 36.00 per share, profit is 92.85000000000036



    I am expecting a output like this (Which I have error running:

    Tracking of Stock : FCS
    8 shares has been brought at $37.61
    Total shares now 8 at total cost $300.88
    33 shares has been brought at $36.31
    Total shares now 41 at total cost $1499.11
    17 shares has been sold at $42.67
    Total shares now 24 at total cost $773.72
    19 shares has been sold at $32.31
    Total shares now 5 at total cost $159.83
    31 shares has been brought at $33.85
    Total shares now 36 at total cost $1209.18
    28 shares has been brought at $36.37
    Total shares now 64 at total cost $2227.54
    20 shares has been brought at $35.49
    Total shares now 84 at total cost $2937.34
    At $36.00 per share, profit is $86.66

  13. #12
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,739
    My Mood
    Mellow
    Thanks
    172
    Thanked 622 Times in 609 Posts

    Default Re: MuliThreading Unable to get outputs but many NPES.....

    Empty catch blocks are pointless, less than worthless. Put something in there to let you know when something has gone wrong and what it was.

    Looking at your last "I am expecting" paragraph, why are you expecting that output? Can you trace why you expect that (or something like it) to happen? Or, you might attack it another way: Explain why the output you are getting is unreasonable or unexpected. First explain either or both approach to yourself, and then let us know what you discovered if you want help unraveling the mystery.

    You say you're getting an error, so you know something's wrong with your code. What is the error? Please post it, copied and pasted into a post.

  14. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,105
    Thanks
    48
    Thanked 2,153 Times in 2,126 Posts

    Default Re: MuliThreading Unable to get outputs but many NPES.....

    A suggestion for testing:
    Change the values assigned to rand1 and rand2 to be constants instead of random so that the math is always the same. That will allow you to work on the threads and not have the execution change because of random values.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Help, text reader outputs high count value!
    By Lashickk in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 31st, 2013, 09:14 PM
  2. Why do I get 0 in the outputs?
    By jean28 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 30th, 2012, 12:00 PM
  3. Code only outputs 1's
    By LoganC in forum What's Wrong With My Code?
    Replies: 7
    Last Post: September 27th, 2012, 09:26 PM
  4. Is there a way I could create/get a program that outputs a form in BBC?
    By Elite Cow in forum Java Theory & Questions
    Replies: 5
    Last Post: October 11th, 2011, 01:01 PM
  5. LinkedList outputs ONLY last element
    By hexwind in forum What's Wrong With My Code?
    Replies: 3
    Last Post: June 30th, 2011, 04:57 AM

Tags for this Thread