473,418 Members | 1,944 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,418 software developers and data experts.

While loop Problem

Expand|Select|Wrap|Line Numbers
  1.         int AmtOfQuotes = 1;
  2.         //A bit of help? The ! at the front means not.
  3.         while (!Quotes[AmtOfQuotes].equals("")){
  4.             AmtOfQuotes++;
  5.         }
  6.  
  7.         AmtOfQuotes--;
  8.  
These are my code (part). When i compile my whole program, i get:
Exception in thread "main" java.lang.NullPointerException
at QuoteGenerator.main(QuoteGenerator.java:40)
Java Result: 1

Can someone help me?
Nov 4 '10 #1
3 1871
Dheeraj Joshi
1,123 Expert 1GB
Are you sure 40th line in QuoteGenerator.java is same as the post you have posted here.?

Regards
Dheeraj Joshi
Nov 4 '10 #2
Atli
5,058 Expert 4TB
I'm guessing it's complaining that there is no Quotes[1], so calling that translates to NULL and thus can not be used as a string.

You need to make sure there is actually an element at the position you are asking for before you try to fetch it. - Simplest way to do that is to compare the index you are trying to fetch to the Array.length property. In your case that would read something like:
if(AmtOfQuotes < Quotes.length)

Also consider that Java arrays are zero-indexed, so when you as for Quotes[1] you are actually asking for the second element. If you want to read the whole array, you need to start with 0.
Nov 4 '10 #3
Dheeraj Joshi
1,123 Expert 1GB
If it has some issues with the array indexes it should throw ArrayIndexOutOfBound exception. Not a null pointer exception.

I guess the value at
Expand|Select|Wrap|Line Numbers
  1. Quotes[1]
is null.

I suggest you to display all the values in the array or if you are using any editors (like eclipse) please follow the value of array during array traverse.

An Atli is right Array in Java is Zero indexed. So you may need to fine tune your loop.

Regards
Dheeraj Joshi
Nov 4 '10 #4

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

Similar topics

0
by: Charles Alexander | last post by:
Hello I am new to php & MySQL - I am trying to retrieve some records from a MySQL table and redisplay them. The data in list form looks like this: Sample_ID Marker_ID Variation ...
0
by: adriann | last post by:
I have a strange problem whereby an attempt at a looped SQL query only passes successfully once. I get the error.. Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result...
1
aprilmae36
by: aprilmae36 | last post by:
Nice day to all... Here I am again, having another problem with loops... lol... Can you please help me make a while loop program that the output will be like this: 1 121 12321 1234321...
14
by: Jan Schmidt | last post by:
Hi, in a nested do-while-loop structure I would like to "continue" the outer loop. With goto this should be no problem in while-loops. However, for do-while I cannot get it to work (without a...
2
by: d3vkit | last post by:
Okay so I can NOT get my while loop to work. It's the most confusing thing I've ever come across. It was working fine and then suddenly, nothing. No error. The page just dies. I am using PHP5 with...
3
by: Patrick C | last post by:
Hey, If i use a try/except clause to get around something like a TypeError, will that immediately bust me out of a while loop? If so, is there any way i can tell it to stay in the while loop...
3
by: Jesse Burns aka jburns131 | last post by:
I've got a coding problem here that I could use some help with. Here's my db class (the config.php only has db connection info): ...
4
by: saytri | last post by:
I have a problem with do while loop. I am displaying a dialogue box where a person should enter the type of quiz he had performed. The problem is that the user should enter only the following 3...
0
by: PythonNotSoGuru | last post by:
Hi everyone. I am using a while loop in a game and during the game loop i want the user to be able to give input without the program stopping at a certain point and asking for the input. In other...
2
by: cmb3587 | last post by:
My task is to take a user input that ranges from 1-50 and use it to create a triangle of asterisks. I.E: Enter number: 5 * ** *** **** ***** ****
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.