Binary search problem

As part of my assignment I have had to write 4 different searching techniques and I am having trouble with my last one as it doesnt seem to be finding the target and so I think there is something wrong with my algorithm could someone please have a look and see if they can see where I am going wrong cheers.

if(e.getSource()==btnFind)

{

pCentre.setVisible(true);

int no = Integer.parseInt(txtStudentNo.getText().trim());

int low,high;

x=0;

low = 0;

high = stud.length-1;

found=false;

while (low <= high)

{

int mid = (low + high) / 2;

if (stud[mid].getStudentNo() < no)

low = mid + 1;

elseif (stud[mid].getStudentNo() > no)

high = mid - 1;

else

found =true;

}

if(found==true)

{

pCentre.setVisible(true);

found =true;

txtTitle.setText(stud[x].getTitle());

txtForename.setText(stud[x].getForename());

txtSurname.setText(stud[x].getSurname());

txtAddress1.setText(stud[x].getAddress1());

txtAddress2.setText(stud[x].getAddress2());

txtAddress3.setText(stud[x].getAddress3());

txtPostcode.setText(stud[x].getPostcode());

txtDob.setText(stud[x].getDob());

txtSex.setText(stud[x].getSex());

txtTelephone.setText(stud[x].getTelephone());

}

else

JOptionPane.showMessageDialog(null,"Student Not Found");

}

[2181 byte] By [JP_Traininga] at [2007-11-26 12:22:20]
# 1
> else> found = true; It would be a good time to get out of the loop at that point too.> txtTitle.setText(stud[x].getTitle());You never set 'x' (except when you initialized it to 0) - hint hint.
warnerjaa at 2007-7-7 15:15:21 > top of Java-index,Archived Forums,Socket Programming...
# 2

I realise that it is set to x and that it is set to zero but this was done just to see if it would show up anything on the screen and it should show the first student in the array but this is not the case.

Can you elaborate further about being a good time to get out of the loop am I not doing it at this stage ?

JP_Traininga at 2007-7-7 15:15:21 > top of Java-index,Archived Forums,Socket Programming...
# 3

> I realise that it is set to x and that it is set to

> zero but this was done just to see if it would show

> up anything on the screen and it should show the

> first student in the array but this is not the case.

I don't know if you mean found is never being set to true by what you said above. "this is not the case" doesn't carry enough meaning.

If found is remaining false, then since your binary search algorithm appears to be on the right track, I'd have to conclude that the array is not already sorted by student number, which it has to be in order for the binary search to work.

> Can you elaborate further about being a good time to

> get out of the loop am I not doing it at this stage ?

You don't check found in the loop, so it keeps on looping as long as low <= high. But it's pointless to keep looping if you found the target item. That would be a good point to put a 'break' statement, or to include the check of 'found' in your while statement. It would also be the right point to set the 'x' variable to the index that you found the match.

warnerjaa at 2007-7-7 15:15:21 > top of Java-index,Archived Forums,Socket Programming...
# 4
Thanks for your help but you have completely lost me I think i will just go to bed and start again in the morningThanksJP
JP_Traininga at 2007-7-7 15:15:21 > top of Java-index,Archived Forums,Socket Programming...
# 5

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

at BinarySearch.actionPerformed(BinarySearch.java:365)

at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)

at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)

at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)

at javax.swing.DefaultButtonModel.setPressed(Unknown Source)

at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)

at java.awt.Component.processMouseEvent(Unknown Source)

at javax.swing.JComponent.processMouseEvent(Unknown Source)

at java.awt.Component.processEvent(Unknown Source)

at java.awt.Container.processEvent(Unknown Source)

at java.awt.Component.dispatchEventImpl(Unknown Source)

at java.awt.Container.dispatchEventImpl(Unknown Source)

at java.awt.Component.dispatchEvent(Unknown Source)

at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)

at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)

at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)

at java.awt.Container.dispatchEventImpl(Unknown Source)

at java.awt.Window.dispatchEventImpl(Unknown Source)

at java.awt.Component.dispatchEvent(Unknown Source)

at java.awt.EventQueue.dispatchEvent(Unknown Source)

at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)

at java.awt.EventDispatchThread.pumpEvents(Unknown Source)

at java.awt.EventDispatchThread.pumpEvents(Unknown Source)

at java.awt.EventDispatchThread.run(Unknown Source)

That is the errors I am getting in the console and when I click on it the line below is highlighted:

if (stud[mid].getStudentNo() < no)

JP_Traininga at 2007-7-7 15:15:21 > top of Java-index,Archived Forums,Socket Programming...
# 6
can someone please help me as I have been working on this for 4 hous straight and am still encountering these same errors
JP_Traininga at 2007-7-7 15:15:21 > top of Java-index,Archived Forums,Socket Programming...
# 7

Ok, here's the exception:

> java.lang.NullPointerException

> at BinarySearch.actionPerformed(BinarySearch.java:365)

And you say it's this line in your code:

> if (stud[mid].getStudentNo() < no)

Then either:

a) stud is null

b) stud[mid] is null

Debug it. For example, if you don't actually run a debugger, you could instead do a "poor-man's debugging technique"...

if (stud == null)

{

System.out.println("OOPS! I never allocated the stud array!");

}

if (stud[mid] == null)

{

System.out.println("OOPS! I never allocated the element in stud at index " + mid + "!");

}

warnerjaa at 2007-7-7 15:15:21 > top of Java-index,Archived Forums,Socket Programming...