Click to See Complete Forum and Search --> : pls help


geniez29
January 21st, 2003, 02:18 AM
is this following code valid?

QMPS.sendLocationRequest("nypsg","nyp654sg",(String)phoneNo.elementAt(phoneNo.size()-1),"2");

QMPS is another file,which i have called here.

is this line valid?
(String)phoneNo.elementAt(phoneNo.size()-1)
wat does elementAt mean and wat does it do?

abramia
January 21st, 2003, 03:01 AM
I would say that if it compiles, then it's valid. Did you try to compile it? [Or am I missing something?]

Assuming you are using JDK 1.4, and assuming that "elementAt" is defined in one of the classes in the JDK, a quick search of the API documentation (javadoc), reveals that there is an "elementAt()" method defined for class "java.util.Vector" and "javax.swing.DefaultListModel". Did you try reading the relevant javadoc (assuming it is relevant -- or am I again missing something)?

Hope this helps you.

Good Luck,
Avi.

saddysans
January 21st, 2003, 03:26 AM
According to ur code, elementAt is a method defined in a class, whose object is phone. It is difficult to say anything more from this much information.
whose code is this anyway, if it is urs than u should be knowing what is elementAt.

saddysan

Goodz13
January 21st, 2003, 08:41 AM
I would say that phoneNo is a Vector. If I'm right, a Vector is simular to an array of Objects. The difference is a Vector is dynamic.

phoneNo.elementAt(phoneNo.size()-1) says give me the last element (Object) in the Vector. Vactor.elementA(int) returns an Object, so (String)phoneNo.elementAt(phoneNo.size()-1) says give me the last object in the Vector and cast it as a String. phoneNo.size() gives you the current number of Elements in the Vector, but don't forget, arrays are 0 based.

geniez29
January 22nd, 2003, 08:21 PM
hi goodz13
thanks for replying..thanks alot..

when i run the follwing program, i get a statement saying: null pointer exception,in the command DOS prompt screen.i use the command DOS prompt to run my programs. wat does that statement mean? why do i get a null pointer exception? my friend actually helped me along with this program,wat does"JbInit" do? can i remove this part of the program,the one with the "try and catch"? sorry for the trouble,i'm new to java programming.



import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.UIManager;
import java.net.URL;
import java.net.HttpURLConnection;
import java.io.*;
import java.util.*;
import java.lang.String;
import se.ericsson.epk.mps.mppapi.mtlr.LocationResult;
import se.ericsson.epk.mps.mppapi.mtlr.MPSException;

public class LocationDetails extends JFrame
{
TableValues tv;
Vector phoneNo;
int selectedRow;
JLabel label1 = new JLabel();
JLabel label2 = new JLabel();
JTextField field1 = new JTextField();
JTextField field2 = new JTextField();

public LocationDetails()
{
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try
{


jbInit();
this.setVisible(true);
}
catch(Exception e)
{
e.printStackTrace();
}
}



private void jbInit() throws Exception
{
label1.setText("Address:");
label1.setBounds(new Rectangle(46, 42, 89, 24));
label2.setText("Postal Code:");
label2.setBounds(new Rectangle(47, 82, 79, 27));

QueryMPS QMPS = new QueryMPS();
QMPS.sendLocationRequest("nypsg","nyp654sg",(String)tv.getValueAt(selectedRow,1),"2");

field1.setText(QMPS.getAddress());
field1.setBounds(new Rectangle(136, 43, 149, 22));
field2.setText(QMPS.getPostCode());
field2.setBounds(new Rectangle(138, 85, 149, 21));

this.getContentPane().setLayout(null);
this.setVisible(true);
this.setSize(350,300);

this.setTitle("Location Details");
this.getContentPane().add(label2, null);
this.getContentPane().add(field1, null);
this.getContentPane().add(field2, null);
this.getContentPane().add(label1, null);
}


protected void processWindowEvent(WindowEvent e)
{
super.processWindowEvent(e);
if(e.getID() == WindowEvent.WINDOW_CLOSING)
{
System.exit(0);
}
}

public static void main(String[] args)
{
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch(Exception e)
{
}
new LocationDetails();
}

}

Goodz13
January 22nd, 2003, 09:18 PM
a NullPointerException means that you are trying to use an object that either hasn't been instantiated or has been set to null.

You need to create an instance of your TableValues tv;
Vector phoneNo objects.

ie in your jbInit

tv = new TableValues();
phoneNo = new Vector();


I suspect that your friend that has helped you, uses Borland JBuilder. jbInit is a method that JBuilder uses for initilation. When you go into design view in JBuilder, it automatically writes that method into your code, and when you add any GUI components to your project, that's where they get initilized. It throws an exception just incase another method that it calls throws an exception, so it doesn't have to try and catch it.

You can take everything from this method and put it in your constructor, remove the try and catch blocks and delete all references to jbInit, if you like. Or you can leave it in. It doesn't do any harm.

I do have a suspicion that QueryMPS throws an MPSException though, so you might want to leave it where it is.

For future reference, it's easier to read code with the Code Tags. [ code ] and [ /code ], without the spaces.

geniez29
January 23rd, 2003, 12:23 AM
hi again,
thanks for your help...i tried putting in the codes u gave me but i still encountered errors such as "cannot resolve symbol for variable tv" and "cannot resolve symbol for constructor TableValues()" and "cannot resolve symbol for phoneNo"..
why is this so?

here is the program with the new codes added in :import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.UIManager;
import java.net.URL;
import java.net.HttpURLConnection;
import java.io.*;
import java.util.*;
import java.lang.String;
import se.ericsson.epk.mps.mppapi.mtlr.LocationResult;
import se.ericsson.epk.mps.mppapi.mtlr.MPSException;

public class LocationDetails extends JFrame
{


int selectedRow;
JLabel label1 = new JLabel();
JLabel label2 = new JLabel();
JTextField field1 = new JTextField();
JTextField field2 = new JTextField();

public LocationDetails()
{
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try
{
jbInit();
this.setVisible(true);
}
catch(Exception e)
{
e.printStackTrace();
}
}



private void jbInit() throws Exception
{
tv = new TableValues();
phoneNo = new Vector();

label1.setText("Address:");
label1.setBounds(new Rectangle(46, 42, 89, 24));
label2.setText("Postal Code:");
label2.setBounds(new Rectangle(47, 82, 79, 27));

QueryMPS QMPS = new QueryMPS();
QMPS.sendLocationRequest("nypsg","nyp654sg",(String)tv.getValueAt(selectedRow,1),"2");

field1.setText(QMPS.getAddress());
field1.setBounds(new Rectangle(136, 43, 149, 22));
field2.setText(QMPS.getPostCode());
field2.setBounds(new Rectangle(138, 85, 149, 21));

this.getContentPane().setLayout(null);
this.setVisible(true);
this.setSize(350,300);

this.setTitle("Location Details");
this.getContentPane().add(label2, null);
this.getContentPane().add(field1, null);
this.getContentPane().add(field2, null);
this.getContentPane().add(label1, null);
}


protected void processWindowEvent(WindowEvent e)
{
super.processWindowEvent(e);
if(e.getID() == WindowEvent.WINDOW_CLOSING)
{
System.exit(0);
}
}

public static void main(String[] args)
{
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch(Exception e)
{
}
new LocationDetails();
}

}

dlorde
January 23rd, 2003, 04:56 AM
You deleted the declarations of 'tv' and 'phoneNo', so the compiler can't find them anymore:

TableValues tv;
Vector phoneNo;

I would recommend that you learn some basic Java before continuing - the Java Tutorial (http://java.sun.com/docs/books/tutorial/) and Bruce Eckel's "Thinking in Java (http://www.codeguru.com/java/tij/)" are good places to start.

If the good die young, how come the pope always lives so long?