Results 1 to 4 of 4

Thread: null values

  1. #1
    jabo is offline Member
    Join Date
    Mar 2010
    Posts
    8
    Rep Power
    0

    Default null values

    I originally had the code written below the main method (the commented out part) to count the awards each of the 5 people(int[]) have but i realized that i couldnt call variables from main method so i made a new method called "countAwards()". However now i am getting nothing but [0,0,0,0,0] when i call countAwards(). Is it because countAwards() happens before the data is passed through? Any help would be appreciated!
    PHP Code:
    import java.awt.*;
    import java.io.*;
    import java.util.Arrays;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    
    public class MainDriver extends JFrame{
    	private JButton jbtShowHistogram = new JButton("Show Histogram");
    	private Histogram histogram = new Histogram();
    	private JFrame histogramFrame = new JFrame();
    	ListOfWinners awardWinners = new ListOfWinners();
    
    	public MainDriver() {
    	    add(jbtShowHistogram, BorderLayout.SOUTH);
    
                jbtShowHistogram.addActionListener(new ActionListener() {
    	         public void actionPerformed(ActionEvent e) {
    	              int[] count = countAwards();
    	              histogram.showHistogram(count);
                          histogramFrame.setVisible(true);
    	         }
    	    });
    
    	    histogramFrame.add(histogram);
    	    histogramFrame.pack();
    	    histogramFrame.setTitle("Histogram");
    	  }
    	private int[] countAwards(){
    		String str;
    	    int[] counts = new int[5];
    	    BufferedReader reader = new BufferedReader(
    	              new StringReader(awardWinners.toString()));
    	    int y= -1; // the number to increment
    	    try {
    			while((str = reader.readLine()) != null){ 
    			  int x = str.indexOf(", ");
    			  if(x != -1)
    			     counts[y]++;
    			  else{
    			     y++; // next numbers to read
    			     counts[y] = 0;
    			   }
    			  System.out.println(str);
    			  System.out.println(Arrays.toString(counts));
    			}
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		return counts;
    	}
    	
    	public static void main(String[] args) throws IOException{
                    ListOfWinners awardWinners = new ListOfWinners();
    		
    		BufferedReader inputReader = new BufferedReader(new InputStreamReader( System.in ));
                    System.out.println("Enter the file name");
                    String input = inputReader.readLine();
    		System.out.println(input + "is the file you selected for the information to be taken from.");
    		awardWinners.readInputData(input);
    		
                    /******
    		String str;
    	        int[] countz = new int[5];
    	        BufferedReader reader = new BufferedReader(
    	              new StringReader(awardWinners.toString()));
    	        int y= -1; // the number to increment
    	        while((str = reader.readLine()) != null){ // read the entire file
    	        int x = str.indexOf(", ");
    	        if(x != -1)
    	        countz[y]++;
    	             else{
    	              y++; // next numbers to read
    	              counts[y] = 0;
    	             }
    	      System.out.println(str);
    	      System.out.println(Arrays.toString(counts));
    	    }
                ******/
    		
    
    		
    	    MainDriver frame = new MainDriver();
    	    frame.setLocationRelativeTo(null); // Center the frame
    	    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	    frame.setTitle("Histogram");
    	    frame.pack();
    	    frame.setVisible(true);
    		
    		
    	    UserInputs.getInput(awardWinners);
    	}
    }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,863
    Rep Power
    19

    Default

    You populate a variable called awardWinners in the main() method. However this is not the same object as the awardWinners in the MainDriver, and used by the countAwards() method.

  3. #3
    jabo is offline Member
    Join Date
    Mar 2010
    Posts
    8
    Rep Power
    0

    Default

    i see what you are saying.
    so i need to use the same awardWinners variable used in the main method in the countAwards() method?
    Last edited by jabo; 03-31-2010 at 03:08 PM.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,863
    Rep Power
    19

    Default

    Well, you could do, but that's not terribly good practice in Java. To do this you could simply make awardWinners static, and then not declare one in main()...just initialise the static one.

    But, as I say, that's not good practice.
    I'd add a loadData() method to the code which does the stuff that's in the first bit of main(), then call that after you've constructed the MainDriver...so frame.loadData().

    This (should) get it working.
    Structure-wise I'd say there would still be problems with how you're writing this, but I suspect you're still in learning-mode, so I'll leave it there.

Similar Threads

  1. Replies: 2
    Last Post: 03-19-2010, 05:30 PM
  2. resting text field values on jsp page to null......
    By Sachin. in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 09-27-2009, 02:24 AM
  3. How to get null values stored in array
    By Ms.Ranjan in forum New To Java
    Replies: 4
    Last Post: 05-21-2009, 10:29 PM
  4. Replies: 0
    Last Post: 05-22-2008, 10:53 AM
  5. Replies: 4
    Last Post: 04-15-2008, 01:04 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
  •