Solved

IF / ELSE: Changing Variable Values

Posted on 2001-06-11
3
158 Views
Last Modified: 2012-05-04
Hello,
I'm getting NullPointerExceptions when i compile my program and I think I'm doing something wrong. So that I don't confuse you I'll give you the code before the exceptions began to happen and tell you what I need it to do. Here's a basic overview of the code: (much of the code has been excluded to keep things simple) (there's an explanation after the code)

class MailGUI extends JFrame
{
JCheckBox altserverCheckBox = new JCheckBox("Use an Alternate Server and/or port");

altserverCheckBox.addActionListener(new java.awt.event.ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
        altsvrBox_actionPerformed(e);
      }
    });

void altsvrBox_actionPerformed(ActionEvent e)
{
serverTextField.setEnabled(altserverCheckBox.isSelected());
portTextField.setEnabled(altserverCheckBox.isSelected());
    if(altserverCheckBox.isSelected()){
    serverTextField.setBackground(bgwhite);
    portTextField.setBackground(bgwhite);}
    else{
    serverTextField.setBackground(bggrey);
    portTextField.setBackground(bggrey);}
}

}

class Message {
  String destination;

  public void setDestination(String dest) {
    destination = dest;
  }
  public String getDestination() {
    return destination;
  }
  public String getDestinationHost() {
    return destination.substring(destination.indexOf('@')+1);
  }
}

class MailTalk {
Message msg;

String destination = msg.getDestinationHost();
}


I want it to work so that if the checkbox defined in MailGui is checked it sets "destination" (String defined in MailTalk) to the text from a text field on the same GUI. If the box isn't checked I want it to be set to what it's currently bieng set to.

If this is confusing, don't be detured, just ask for clarification. Thanks.
0
Comment
Question by:m_morgan
3 Comments
 
LVL 4

Accepted Solution

by:
ykf2000 earned 50 total points
Comment Utility
try modifying to this:

public String getDestinationHost() {
   return destination.equals("") ? "" : destination.substring(destination.indexOf('@')+1);
 }
0
 
LVL 9

Expert Comment

by:Ovi
Comment Utility
where do you declare portTextField and serverTextField ? I cannot see'it anyware. If you don't declare'it you should do'it. Any way, post the full exception
0
 

Author Comment

by:m_morgan
Comment Utility
Ovi: the exception is not the issue it's what to do instead. I did declare the two text fields as well as a bunch of other stuff that I left out.

ykf2000: that code compiles but when the function is called the program does nothing. Was I supposed to substitute certian things for names of parts of my program? Could you give me the code if I wanted it to decide where to go based on whether the: altserverCheckBox is checked. If it isn't checked I want it to return this: destination.substring(destination.indexOf('@')+1). If it is checked I want it to return the value of whatever is in the serverTextField.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
commonTwo  challenge 63 98
noX challenge 17 75
maven java path setting 5 49
Problem to start Neon 20 49
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues undeā€¦
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilationā€¦
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now