8 Replies - 6427 Views - Last Post: 16 March 2011 - 06:01 PM Rate Topic: -----

#1 paulple  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 13
  • Joined: 13-March 11

Adding contents to an array of label JAVA

Posted 15 March 2011 - 05:55 AM

How can I add contents to an array of label

Here is the code:


for (int i = 1; i < tbt; i++){
			int q = Min(bt, at,tbt, i,n);				
			if (q != q2){
				
			
				label[q].setText("ASD");
				jPanel[q].add(label[q]);
				jDialog1.add(jPanel[q]);
			
				time[k++] = i;
				wt[q] = i;
				tat[q] = i + bt[q];
				
				
			}
			
			bt[q] = bt[q] - 1;
			q2 = q;
		}




Error:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at SrtfAndRrGui.SrtfAndRrGui.SrtfSelected(SrtfAndRrGui.java:190)
at SrtfAndRrGui.SrtfAndRrGui.access$100(SrtfAndRrGui.java:18)
at SrtfAndRrGui.SrtfAndRrGui$2.actionPerformed(SrtfAndRrGui.java:72)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6263)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
at java.awt.Component.processEvent(Component.java:6028)
at java.awt.Container.processEvent(Container.java:2041)
at java.awt.Component.dispatchEventImpl(Component.java:4630)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4574)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
at java.awt.Container.dispatchEventImpl(Container.java:2085)
at java.awt.window.dispatchEventImpl(window.java:2475)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)



Am I missing some parts or there's something wrong with it?

Is This A Good Question/Topic? 0
  • +

Replies To: Adding contents to an array of label JAVA

#2 masijade  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 196
  • View blog
  • Posts: 580
  • Joined: 03-April 10

Re: Adding contents to an array of label JAVA

Posted 15 March 2011 - 06:19 AM

Looks as though you have never defined the elements of the array. When you initialise an array of objects (regardless of what class) you need to initialise the individual elements before you can use tham as that object, as initially, per default, all elements will contain a null reference value. I.E. for every index you must do array[index] = new Whatever()

This post has been edited by masijade: 15 March 2011 - 06:20 AM

Was This Post Helpful? 0
  • +
  • -

#3 b0ng01  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 38
  • View blog
  • Posts: 169
  • Joined: 16-July 10

Re: Adding contents to an array of label JAVA

Posted 15 March 2011 - 06:20 AM

You aren't giving nearly enough information to be helpful in helping us figure out wtf is wrong with your code. Either post a *full* test case or give us the entire code. It is rather hard to try and fix a flat when you don't know where the hole is located.
Was This Post Helpful? 0
  • +
  • -

#4 paulple  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 13
  • Joined: 13-March 11

Re: Adding contents to an array of label JAVA

Posted 15 March 2011 - 06:35 AM

I appologize for the incomplete code

here it is:
package SrtfAndRrGui;

public class SrtfAndRrGui extends javax.swing.JFrame {
	private int n;
	

    /** Creates new form SrtfAndRrGui */
    public SrtfAndRrGui() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jComboBox1 = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jComboBox2 = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        jDialog1 = new javax.swing.JDialog();
        jDialog2 = new javax.swing.JDialog();
		label = null;
		jPanel = null;
		
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4", "5", "6",
        																	    "7", "8", "9", "10", "11", "12", 
        																	    "13", "14", "15", "16", "17",
        																	    "18", "19" }));
        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                NoOfProcess(evt);
                
            }
        });

        jLabel1.setText("Processes: ");

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jTextArea1.setText("PID\tAT\tBT\tWT\tTT\n");
        jScrollPane1.setViewportView(jTextArea1);
		jTextArea1.setEditable(false);
        jButton1.setText("SRTF");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	SrtfSelected(evt);
            	             
            }
            
            
        });

        jButton2.setText("RR");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                RrSelected(evt);
            }
        });
        
        jLabel2.setText("TQ:");
        
//		label[n].setBackground(new java.awt.Color(51, 255, 255));
//		label[n].setFont(new java.awt.Font("Bauhaus 93", 1, 36));
		
        jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4" }));
        jComboBox2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                TqSelected(evt);
            }
        });
        jLabel2.setText("Processes: ");
        
      
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(30, 30, 30)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jButton2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jButton1, javax.swing.GroupLayout.Alignment.LEADING))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)                     
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)))	
                .addGap(22, 22, 22)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 244, Short.MAX_VALUE)
                .addGap(92, 92, 92))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, 0, 0, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(17, 17, 17)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)                                                    
                            .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))                            
                        .addGap(18, 18, 18)
                        .addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jButton2)                            	
                            .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(179, 179, 179))
        );

        pack();
    }// </editor-fold>
	
    private void NoOfProcess(java.awt.event.ActionEvent evt) {                             
        // TODO add your handling code here:
        javax.swing.JComboBox noOfP = (javax.swing.JComboBox)evt.getSource();
		String cast_str = noOfP.getSelectedItem().toString();
		n = Integer.parseInt(cast_str);
    }


    private void SrtfSelected(java.awt.event.ActionEvent evt) throws ArrayIndexOutOfBoundsException {
        // TODO add your handling code here:
 		jDialog1.setVisible(true);
        int p[] = new int[n];
		int at[] = new int[n];
		int bt[] = new int[n];
		int bt2[] = new int[n];
		int wt[] = new int[n];
		int tat[] = new int[n];
		
		int count = 1;
		
		for (int i = 0; i < n; i++)	{

			p[i] = count;
			at[i] = 0 + (int)(Math.random()*10);
			bt[i] = 4 + (int)(Math.random()*20);
			bt2[i] = bt[i];
			at[0] = 0;

			count++;
		}
		
		int tbt=0;
		for (int i = 0; i < n; i++){
			tbt = tbt + bt[i];
		}
		int time[] = new int[tbt];
		int k = 0;
		int q2 = 0;
		
		bt[0] = bt[0] - 1;
		for (int i = 1; i < tbt; i++){
			int q = Min(bt, at,tbt, i,n);				
			if (q != q2){

				
//**************************************************		//**************************************************	
// THIS IS WHERE THE ERROR GENERATES
				label[q].setText("ASD");
				jPanel[q].add(label[q]);
				jDialog1.add(jPanel[q]);
		//**************************************************
//**************************************************


	
				time[k++] = i;
				wt[q] = i;
				tat[q] = i + bt[q];
				
				
			}
			
			bt[q] = bt[q] - 1;
			q2 = q;
		}
		time[k] = tbt;
		double wtime = 0;
		double tatime = 0;

		for (int i = 0; i < n; i++)	{
			wt[i] = wt[i] - at[i];
			tat[i] = tat[i] - at[i];
			wtime = wt[i];
			tatime = tat[i];
		}
		
		for (int i = 0; i < n; i++){
			jTextArea1.append(p[i] + "\t " + at[i] + "\t " + bt2[i] + "\t"
					  + wt[i] + "\t" + tat[i] + "\n");
		}
		jTextArea1.append("\nAvg waiting time =" + (wtime / n) +"\n"+ "Avg turnaround time = " + (tatime / n) + "\n");
 		

    }

    private void RrSelected(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
        
        
    }

    private void TqSelected(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new SrtfAndRrGui().setVisible(true);
            }
        });
    }
    
	public static int Min(int b[], int a[],int tbt, int r,int n){
		int j = 0;
		int min = tbt;

		for (int i = n - 1; i >= 0;i-- ){
			if (b[i] < min && b[i] > 0 && r >= a[i]){
				min = b[i];
				j = i;
			}
		}
		return j;
	}
        
    // Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JDialog jDialog1;
    private javax.swing.JDialog jDialog2;
    private java.awt.Label label[];
    private javax.swing.JPanel jPanel[];
    // End of variables declaration

}



I used Netbeans in making the design thats why its kinda confusing :D

The computations are not correct.
Don't mind them :D

This post has been edited by paulple: 15 March 2011 - 06:45 AM

Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10809
  • View blog
  • Posts: 40,288
  • Joined: 27-December 08

Re: Adding contents to an array of label JAVA

Posted 15 March 2011 - 08:32 AM

Don't use the NetBeans GUI Builder. Seriously write your own GUI code by hand. Swing is pretty straight-forward and easy to use. You can check out the DIC Java Tutorials Section for Swing tutorials, if you want.
Was This Post Helpful? 1
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,910
  • Joined: 06-March 08

Re: Adding contents to an array of label JAVA

Posted 15 March 2011 - 06:40 PM

Where is your label[] array initialized ?
The only thing I see is
label = null;

and don't use a GUI generator. It produces horrible code almost unreadable by a human being. We won't waste our time tryng to decipher it. The problem is probably not in the code but in the instructions you gave to the GUI generator and we can't be behind your shoulders watching what you do.
Was This Post Helpful? 1
  • +
  • -

#7 masijade  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 196
  • View blog
  • Posts: 580
  • Joined: 03-April 10

Re: Adding contents to an array of label JAVA

Posted 16 March 2011 - 12:17 AM

Correct, he not only has not initialised the elements of the array he hasn't even defined the array itself, only declared it.
Was This Post Helpful? 0
  • +
  • -

#8 paulple  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 13
  • Joined: 13-March 11

Re: Adding contents to an array of label JAVA

Posted 16 March 2011 - 02:11 AM

oh. I'll just figure it out.

well yeah I too can't understand some parts the netbeans created.
by the way, thanks for your advises :)
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,910
  • Joined: 06-March 08

Re: Adding contents to an array of label JAVA

Posted 16 March 2011 - 06:01 PM

View Postpaulple, on 16 March 2011 - 04:11 AM, said:

well yeah I too can't understand some parts the netbeans created.

In the real world, you will never see GUI generated code going into production. As prototypes sometimes it may be usefull.
Imagine at your first Java programmer job when they will ask you to write a simple GUI using Notepad :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1