
Threads
Chris Roberts
Greenhorn
Joined: Sep 03, 2004
Posts: 8
posted
Oct 11, 2004 15:51:00
0
I was trying to run three threads at the same time. The threads are called Bubble Sort, Selection Sort, and Insertion Sort. I have created at vector that is suppossed to create random numbers(it does) and a DisplayPanel that is supposed to display the sorted threads in my created frame. I kept getting an error at run time saying " Exception in thread "Thread-2"
java.lang.NullPointerException
at Sort$BubbleSort.run(sort.java:193). I don't know whats going on can someone help. Here is my code only for my display panel and BubbleSort() method for reference.
private class DisplayPanel extends JPanel { /** * constructor for display panel. Picks a random number of values * between 10 and 30 and generates that number of random integers * to go in the vector */ private DisplayPanel() { // create the Vector. It is empty until elements added v = new Vector(); // v = new Vector<Integer>()); // generate number of elements to create numElements = (int)Math.round(Math.random()*20)+10; // create numElements different values and put in vector for (int i = 0; i < numElements; i++) v.add(new Integer(randomNumber0To999())); // set the size of the panel based on number of elements setSize(v.size()*30+20,50); } /** * Generate a random number from 0..999. Not too accurate * but for our purposes it does not really matter. * * @returns a random number from 0 to 999 */ private int randomNumber0To999() { return (int) Math.round(Math.random()*999); } /** * Redraws the screen after the panel has been covered, minimized, etc. * Note that this method is never called directly * * @param g Graphics instance to draw on */ public void paintComponent(Graphics g ) { int x = 50; // X-position where you draw int y = 30; // Y-Position where you draw // *always* call the base paintComponent or weird stuff happens super.paintComponent(g); // While not end of the vector, draw box and put number in it for (int i = 0; i < v.size(); i++) { // do white background with black around edges g.setColor(Color.blue); g.fillRect(x, y, 30, 20); g.setColor(Color.yellow); g.drawRect(x, y, 30, 20 ); // Get the value from the vector and draw in the box g.drawString(((Integer)v.elementAt(i)).toString(), x+3, y+19); // increment x so that next box is drawn beside the one // we just did x += 30; } } /** * returns the preferred size of the component which is dependent * on the number of elements in the vector * * @return dimension object representing preferred size */ public Dimension getPreferredSize() { return new Dimension(v.size()*30 + 20, 50); } } DisplayPanel b1; // Bubble Sort private class BubbleSort extends Thread { public BubbleSort(DisplayPanel b){ super(); b1 = b; // b1.repaint(); } public void run() { //try //{ System.out.println("n= "+n); for(i=0; i< numElements -1; i++) { System.out.println("inside run\n"); //for ( j=0; j<n-1-i; j++) for(j=0; j< numElements -1 -i; j++) if (a[j+1] < a[j]) /* compare the two neighbors */ { tmp = a[j]; /* swap a[j] and a[j+1] */ a[j] = a[j+1]; a[j+1] = tmp; System.out.println(v); } } // System.out.println(v); b1.repaint(); }
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24148
27
I like...
posted
Oct 11, 2004 16:07:00
0
Well, the only thing in BubbleSort.run() that could be null would be the array variable "a". It looks like you grab that from the enclosing class -- I don't see where the array is declared or initialized, though. So look at your code, and make sure that "a" is getting set to non-null at some point before you start the BubbleSort thread.
[Jess in Action]
[AskingGoodQuestions]
Consider Paul's
rocket mass heater
.
subject: Threads
Similar Threads
GeneralPath don't works
dynamic Drawing -->Vector ?
Drawing graphs
Drawing in Graphic-Context of a JPanel in a JPanel
JFormattedTextField
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter
JForum
|
Paul Wheaton