Quantcast

Maximum PC

It is currently Wed Nov 26, 2014 11:22 pm

All times are UTC - 8 hours




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Java Build failure
PostPosted: Thu Sep 18, 2008 2:39 pm 
Team Member Top 1000
Team Member Top 1000
User avatar

Joined: Wed Jul 02, 2008 1:41 pm
Posts: 252
Hi, i'm in like my 3rd wek of my AP computer science class and this is a project we finished about a week ago, but I decided to overachieve and am having a problem compiling.

Code:
/*
* Spring.java
*
* Created on September 15, 2008, 10:24 AM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
/**
*
* @author teacher
*/
public class Spring extends JApplet
        implements ActionListener
{
   
    public void init()
    {       
        JButton button = new JButton("Spring");
        button.addActionListener(this);
        Container c = getContentPane();
        c.setBackground(Color.green);
        c.add(button, BorderLayout.SOUTH);
    }
   
    public void actionPerformed(ActionEvent e)
    {
        Random r = new Random();
        Color randColor = new Color(r.nextInt(256), r.nextInt(256), r.nextInt(256));
        c.setBackground(randColor);
    }
}


the last line is where it's failing. Also, I;m using both netbeans 5 (at school) and netbeans 6.1 (at home) as an IDE.


Top
  Profile  
 
 Post subject:
PostPosted: Thu Sep 25, 2008 11:36 am 
8086
8086

Joined: Thu Sep 25, 2008 11:15 am
Posts: 2
You didn't say what the compiler error was specifically, but I'm betting it will say something like "undefined variable c".

In general methods can only access variables that are either passed to them as a parameter, declared in that method, or are global to the class. If we look closely at your code for your actionPerformed method we find that the variable c doesn't satisfy any of these conditions, and hence we cannot perform any actions on it.

c is declared in your init method which means that it can only be used within that method (init). This is why you get an error when you try to use it in the actionPerformed method. For further reading on Scope see this page http://en.wikiversity.org/wiki/Introduction_to_Programming/Scope or google OOP/programming scope.

More to the point, to fix your error we need a way to access c in actionPerformed. One solution to this problem is to give c global scope using the following code
Code:
/*
* Spring.java
*
* Created on September 15, 2008, 10:24 AM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
/**
*
* @author teacher
*/
public class Spring extends JApplet
        implements ActionListener
{
   //By declaring c here (in global scope) all methods within this class can
  //access the c variable
   Container c;


    public void init()
    {       
        JButton button = new JButton("Spring");
        button.addActionListener(this);

        //We still want to initialize c here, but it's already been declared so we
       //can remove the "Container" from this line
        c = getContentPane();

        c.setBackground(Color.green);
        c.add(button, BorderLayout.SOUTH);
    }
   
    public void actionPerformed(ActionEvent e)
    {
        Random r = new Random();
        Color randColor = new Color(r.nextInt(256), r.nextInt(256), r.nextInt(256));
        c.setBackground(randColor);
    }
}

The only caveat I can see with this code is that you must make sure that your init method is called before the actionPerformed method. If it's not then you'll probably get a null-pointer exception at runtime because c won't have been initialized before you try to use it.

I hope this helps.

Gox


Top
  Profile  
 
 Post subject:
PostPosted: Thu Sep 25, 2008 1:15 pm 
Team Member Top 1000
Team Member Top 1000
User avatar

Joined: Wed Jul 02, 2008 1:41 pm
Posts: 252
thanks, I'm honoured to have your first post. but I semi fixed the problem and by now I figure I may as well pay attention in class instead of dealing with a 50 line program.


Top
  Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 8 hours


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group

© 2014 Future US, Inc. All rights reserved.