0

Can anyone see why the following XML file should throw NullPointerexception?

If I remove all reference to the array hrs_to_wait_value, it is ok.

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="hrs_to_wait_options">
<item>1 hour</item>
<item>2 hours</item>
<item>3 hours</item>
<item>4 hours</item>
<item>5 hours</item>
<item>6 hours</item>
<item>7 hours</item>
<item>8 hours</item>
<item>9 hours</item>
<item>10 hours</item>
<item>11 hours</item>
<item>12 hours</item>
<item>13 hours</item>
<item>14 hours</item>
<item>15 hours</item>
<item>16 hours</item>
<item>17 hours</item>
<item>18 hours</item>
<item>19 hours</item>
<item>20 hours</item>
</string-array>

<array name="hrs_to_wait_values">
<item>1</item>
<item>2</item>
<item>3</item>
<item>4</item>
<item>5</item>
<item>6</item>
<item>7</item>
<item>8</item>
<item>9</item>
<item>10</item>
<item>11</item>
<item>12</item>
<item>13</item>
<item>14</item>
<item>15</item>
<item>16</item>
<item>17</item>
<item>18</item>
<item>19</item>
<item>20</item>
</array>


</resources>
5
  • oh it stripped out the xml stuff! Commented Nov 20, 2010 at 20:51
  • You need to use the "code" icon to put your xml and make it visible to us. (icon with 101010 above the writing panel). Or add 4 spaces before each line Commented Nov 20, 2010 at 20:56
  • 1
    It's not your XML file that's doing it, where's your code? Commented Nov 20, 2010 at 20:56
  • 2
    XML doesn't throw exceptions. Java does. Show some code and a stacktrace. Commented Nov 20, 2010 at 21:11
  • I'm sorry but I do not agree guys, this xml does throw an NPE if you try loading it in a spinner for instance... Commented Nov 20, 2010 at 21:27

1 Answer 1

1

Maybe am I mistaken, but I think that your issue is caused indeed by your XML. You wrote this for the first array :

<string-array name="hrs_to_wait_options">
<item>1 hour</item>
<item>2 hours</item>
.
.
.
</string-array>

And, this for the 2nd :

<array name="hrs_to_wait_values">
<item>1</item>
<item>2</item>
.
.
.
</array>

And you actually don't have the "string-" in the name. As I assume that you are using this in some kind of spinner or something of the sort, you have to use string-arrays If i remember well. Try changing it to this :

<string-array name="hrs_to_wait_values">
<item>1</item>
<item>2</item>
.
.
.
</string-array>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks - changing to a string array stopped the exception. I based the xml on an example in Reto Meier's book on Android 2 where he uses the string array for the data to be displayed by a list but an array for the corresponding values which are ints. I can live with it though! Thanks again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.