-5
a=10.00;
b=11.00;
mor_names=new String[]{};   
for(double cal=Double.parseDouble(a);cal<=Double.parseDouble(b);) {
    mor_names[val]=(Double.toString(cal));
    System.out.println("values are:" + mor_names[val]);
    val++;
    cal=cal+0.15;
}

I try to add a value to string array but it raises a null pointer exception, why?

EDIT 1:

DateFormat time = new SimpleDateFormat("HH:mm");
try
{
    timefrom =(Time) time.parse(json.get(position).toString());
    timeto = (Time) time.parse(json.get(position).toString());
} 
catch (ParseException e)
{
    e.printStackTrace();
}

Calendar cal = Calendar.getInstance();

System.out.println("times are :"+timefrom+" "+timeto+" "+position);//null null 0 was output for this

for(Time i=timefrom;i.equals(timeto);)
{
    cal.setTime(i);
    System.out.println("values are:" + i);
    mor_names[val]=(String) i.toString();
    cal.add(Calendar.MINUTE, 15);
    System.out.println("values are:" + mor_names[val]);
    String newTime = df.format(cal.getTime());
    try
    {
        i=new java.sql.Time(time.parse(newTime).getTime());
    } 
    catch (ParseException e)
    {
        e.printStackTrace();
    }
    val++;
}

I tried to get a date in particular time gap so I am trying to sink with java calendar and manipulate my code but possibly I cant get it right.

Naman Gala
  • 4,670
  • 1
  • 21
  • 55
grit sys
  • 3
  • 3

2 Answers2

1

Let's review your code:

a=10.00;
b=11.00;

From that we can assume that a and b are type of Double, so you can't use double cal = Double.parseDouble(a) to get a double value. This would be correct if a and b was of type String:

String a="10.00";
String b="11.00";

The next line:

mor_names=new String[]{};

will initialize the empty array of Strings. The {} parentheses are used with arrays like so to initialize array with values:

String[] mor_names = {"Hello", "World", "!"};

and this is the same like:

String[] mor_names = new String[]{"Hello", "World", "!"};

You want to assign values to array, so you could initialize your array like that:

String[] mor_names = new String[7]{};

with that you can't store more values in mor_names than 7 with indexes from 0 to 6.

If you want more flexible approach use ArrayList:

ArrayList<String> mor_names = new ArrayList<String>();

this will create a "dynamic array" where you can add, remove and get your String objects.

Loop for:

for(double cal = Double.parseDouble(a) ; cal<=Double.parseDouble(b) ; )

Lets fix one thing just like we mentioned before, assuming that a and b are type of double.

for(double cal = a ; cal <= b ; )

First, cal variable is initialized with value of a. For is a loop and it will repeat code between {} parentheses until cal <= b is true. You are incrementing cal value inside this loop, but that is unnecessary - you can do it like this:

for(double cal = a ; cal <= b ; cal+=0.15)

That means that each time the code inside loop has been executed to the end, cal will be incremented by 0.15. Then if cal is less or equal b the code will repeat. Otherwise the loop will end.

NullPointerException would rise if you are trying to assign String value to uninitialized array. But that is not what is happening here, because mor_names is not null.

You should read about initializing variables and using arrays and containers in java. Good luck.

EDIT 1:

You are giving incomplete data. You should provide all things that makes your question solid, especially strict and specific description of what you are trying to archive.

Read about How to parse JSON in Java and Java - Date & Time. Use google and read with understanding.

Also about using stackoverflow you should start here.

One more time, good luck.

Community
  • 1
  • 1
Zaimatsu
  • 66
  • 15
0
String a="10.00";
String b="11.00";
int val=0;
String [] mor_names=new String[10];
for(double cal=Double.parseDouble(a); cal<=Double.parseDouble(b);){
   mor_names[val]=(Double.toString(cal));
   System.out.println("values are:" + mor_names[val]);
   val++;
   cal=cal+0.15;
}

you can use this code.if you want dynamic string array,you should use list or map.

Karthik
  • 4,950
  • 6
  • 35
  • 65
tarikfasun
  • 314
  • 6
  • 16