Doubt with Exceptions
I have string like following:
String line ="192.xxx.xxx.xxx:100 200 192.xxx.xxx.xxx"
If i split this string with space, then i will get three substrings. Now i have this code logic.
1. Split by spaces - returns string array
2. String arrays first element - tokenize with : this returns ip & port
Now my doubt is , the above two statements throws ArrayIndexOutOfBoundsException. My string line may or may not have ip:port substring. If first substring doesnt have ip&port, then it throws ArrayIndexOutOfBoundsException , this skips the second (2) statement. How can i ensure that all statements are executed even if there is any statement throws ArrayIndexOutOfBoundsException
Hope i am clear with my doubt
[782 byte] By [
ysrpa] at [2007-11-27 3:33:28]

well you can for example...1) validate the string, see if the character you tokenize with exists2) catch the exception inside your loop
> well you can for example...
>
> 1) validate the string, see if the character you
> tokenize with exists
> 2) catch the exception inside your loop
Bad me. did get this thought. Thanks
I have another basic doubt. I am just getting confusing with this simple doubt.
Say i have following:
String cm = checkField("port")
if(cm == null) {
...//do something
}
else {
....//actual logic
}
checkField() function takes a string does some search, if found returns a string as success else returns null. Even i am checking the string if its equal to null, i still get NullPointerException. I am just confused or missing somethign here....
ysrpa at 2007-7-12 8:36:33 >

You can't get an NPE by comparing to null (e.g. mystring == null or mystring != null). If that is what you are doing, the NPE is somewhere else in your code.
Usually, you will see a line number in the stacktrace that indicates the line on which the Exception occurs. An NPE cannot occur on the line "if(cm == null)", so it must be somewhere else.
Hi ysrp,
check this code.It does what u need.
import java.util.StringTokenizer;
public class StringToken_ex {
/**
* @param args
*/
public static void main(String[] args) {
String line = "192.xxx.xxx.xxx:100 200 192.xxx.xxx.xxx";
//separating the string with spaces.
String parts[] = null;
parts= line.split(" ");
System.out.println(" show");
for(int i = 0; i< parts.length ; i++){
System.out.println(i + " = " + parts );
}
if (checkPort(parts[0])){
StringTokenizer st = new StringTokenizer(parts[0],":");
while (st.hasMoreTokens()) {
System.out.println(st.nextToken());
}
}
}
private static boolean checkPort(String port){
int index = port.indexOf(":");
boolean portExists = false;
if (index != -1){
portExists = true;
}
return portExists;
}
}
> You can't get an NPE by comparing to null (e.g.
> mystring == null or mystring != null). If that is
> what you are doing, the NPE is somewhere else in your
> code.
>
> Usually, you will see a line number in the stacktrace
> that indicates the line on which the Exception
> occurs. An NPE cannot occur on the line "if(cm ==
> null)", so it must be somewhere else.
Sorry for delayed reply. The exception line number is where i have this if(cm == null) condition. Please see my stack trace
java.lang.NullPointerException
at PortsNetworking.initComponents(PortsNetworking.java:570)
at PortsNetworking.<init>(PortsNetworking.java:21)
at EditConfig.createPortsNetworkPage(EditConfig.java:129)
at EditConfig.<init>(EditConfig.java:80)
javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1849)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2169)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258)
at javax.swing.AbstractButton.doClick(AbstractButton.java:302)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:1000)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:1041)
at java.awt.Component.processMouseEvent(Component.java:5488)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3126)
at java.awt.Component.processEvent(Component.java:5253)
at java.awt.Container.processEvent(Container.java:1966)
at java.awt.Component.dispatchEventImpl(Component.java:3955)
at java.awt.Container.dispatchEventImpl(Container.java:2024)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3892)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822)
at java.awt.Container.dispatchEventImpl(Container.java:2010)
at java.awt.Window.dispatchEventImpl(Window.java:1774)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
The line number 570 is where i have this if(cm == null) condition.
I still have the same problem. Please guide me
ysrpa at 2007-7-12 8:36:33 >

> Hi ysrp,
> check this code.It does what u need.
>
> import java.util.StringTokenizer;
> public class StringToken_ex {
>
> /**
> * @param args
> */
> public static void main(String[] args) {
> String line = "192.xxx.xxx.xxx:100 200
> 00 192.xxx.xxx.xxx";
> //separating the string with spaces.
> String parts[] = null;
> parts= line.split(" ");
> System.out.println(" show");
> for(int i = 0; i< parts.length ; i++){
> System.out.println(i + " = " + parts );
> }
> if (checkPort(parts[0])){
> StringTokenizer st = new
> new StringTokenizer(parts[0],":");
>while (st.hasMoreTokens()) {
> System.out.println(st.nextToken());
>}
> }
> }
>
> private static boolean checkPort(String port){
> int index = port.indexOf(":");
> boolean portExists = false;
> if (index != -1){
> portExists = true;
> }
> return portExists;
> }
>
> }
I will try this. Thanks
ysrpa at 2007-7-12 8:36:34 >

@Bhannat: I tried the sample code and i didnt get any problem. It worked. But when i modified my code as you said, i still get same Nullpointerexception.
ysrpa at 2007-7-12 8:36:34 >

hey man,plz paste ur code..Message was edited by: Bhannat
> hey man,
> plz paste ur code..
>
Part of the code where i get error
Properties p = new Properties();
String filepath = System.getProperty("user.dir") + "/tmp/xyz.conf";
try {
FileInputStream fstream = new FileInputStream(filepath);
p.load(fstream);
String cm = p.getProperty("mem").trim();
if(cm == null) { //no error here
UsageLimit1.setSelected(true);
}
else {
UsageLimit2.setSelected(true);
String arr[] = cm.split(" ");
if(arr != null) { //no error here
if(arr[0] != null) { //error nulpointerexception here
UsageTextField.setText(arr[0]);
}
if(arr[1] != null) {
CachedObjectSize.setSelectedItem(arr[1]);
}
}
}
Please see the comments inside the code where i am getting error
ysrpa at 2007-7-12 8:36:34 >

hi da,are u able to read from the filestream properly?i feel the string is not being retrieved from the file...
> hi da,
>
> re u able to read from the filestream properly?
> i feel the string is not being retrieved from the
> file...
Yes i am able to read from filestream. My question is when i check the condition whether if it is not null and string is empty, its throwing NPE. When it is not null then only condition works or else NPE? I am confused
ysrpa at 2007-7-12 8:36:34 >

How many elements are in the array? The array may not be null, but its length could be 0. In this case, you would try to get a non-existing object.
> How many elements are in the array? The array may not
> be null, but its length could be 0. In this case, you
> would try to get a non-existing object.
Array may or may not have elements. But wait, yes you are correct. I think i should check if array size is not zero then only do something. Am i correct?
If so, let me give a try. Thanks for the tips
ysrpa at 2007-7-12 8:36:34 >
