473,385 Members | 1,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Simple Bubble Sort using strings

17
Hi,
I have to sort customer names in a customer class i have created. Now, there is a method called getName() that basically returns the string name stored in an instance variable of the customer class.
This is a bubble sort method I have written. I really cant figure out why this throws a runtime null pointer exception at the IF statement which compares the strings. Please help.

Expand|Select|Wrap|Line Numbers
  1. private Customer[] bubbleSort(Customer c[])
  2. {
  3.   boolean swaped = true;
  4.   Customer temp;
  5.  
  6.   if (c != null)
  7.   {
  8.     while(swaped)
  9.     {
  10.       swaped = false;
  11.  
  12.       for (int i = 0; i < c.length - 1; i++)
  13.       {
  14.         if (c[i].getName().compareTo(c[i + 1].getName()) > 0)
  15.         {
  16.           temp = c[i];
  17.           c[i] = c[i + 1];
  18.           c[i + 1] = temp;
  19.           swaped = true;
  20.         }
  21.       }
  22.     }
  23.   }
  24.   return c;
  25. }
Thanks
Mar 13 '08 #1
5 3212
Bubbs
17
Same Null pointer exception at the printArray(), that prints the toString of each object in the array to a JTextArea called infoArea. The exception is at the line
infoArea.setText(c[i].toString());

Heres the code
private void printArray(Customer c[])
{
if (c != null)
{
for(int i = 0; i < c.length; i++)
{
infoArea.append(c[i].toString());
}
}
}
Thanks
Mar 13 '08 #2
JosAH
11,448 Expert 8TB
Well, the at least one of your c[i] elements is null and the error must be found
somewhere else.

kind regards,

Jos
Mar 13 '08 #3
r035198x
13,262 8TB
Yep, either one of the c[i]s is null or infoArea itself is null.
You could print each of them before you "dot" them to catch the culprit.
Mar 13 '08 #4
Bubbs
17
thanks guys, much appreciated.
Mar 13 '08 #5
BigDaddyLH
1,216 Expert 1GB
Please enclose your posted code in [code] tags (See How to Ask a Question).

This makes it easier for our Experts to read and understand it. Failing to do so creates extra work for the moderators, thus wasting resources, otherwise available to answer the members' questions.

Please use [code] tags in future.

MODERATOR
Mar 13 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

13
by: Gram | last post by:
Hello, Can anyone help out with a bubble sort for strings using ASP? I have a text file of words id like to sort and count. Thanks in advance for any help. Gram.
10
by: sam | last post by:
hi, i am new to C/C++ and have just finished a program based on the UK national lottery, where you enter 6 numbers, six are generated by the computer and there are appropriate messages and a...
34
by: Mark Kamoski | last post by:
Hi-- Please help. I need a code sample for bubble sort. Thank you. --Mark
4
by: Chris | last post by:
I have a bubble sort for a 2-dimensional array that sorts a string,number pair based on the number. The code for the sort is as follows: Private Sub SortArray(ByRef roundarray(,) As String)...
0
by: Slowjam | last post by:
How do I correct the program below to search all the anagrams in a given file. First, for each word, build another word (its key) with all its letters sorted. For instance, build "dorw" for...
1
by: guest | last post by:
I am doing a program for Intro to Computer Programming where we take an array of strings and we must sort them alphabetically. we are supposed to use a bubble sort, but i know the code if meant for...
12
by: midknight5 | last post by:
Hello everyone, I am familiar with a normal bubble sort when dealing with an array of number but I am unsure as how to implement a sort when I have an array that is filled with classes which hold...
14
by: xtheendx | last post by:
I am writing a gradbook type program. It first allows the user to enter the number of students they want to enter. then allows them to enter the first name, last name, and grade of each student. The...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.