Solved

IF / ELSE: Changing Variable Values

Posted on 2001-06-11
3
182 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 4

Accepted Solution

by:
ykf2000 earned 50 total points
ID: 6180014
try modifying to this:

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

Expert Comment

by:Ovi
ID: 6180370
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
ID: 6183254
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn different types of Android Layout and some basics of an Android App.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
The viewer will learn how to implement Singleton Design Pattern in Java.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

717 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