Input: First line contains an integer denoting the test cases 'T'. T testcases follow. Each testcase contains two lines of input. First line contains N the size of the array A. The second line contains the elements of the array. Example: Input: 2 3 3 2 1 4 1 2 3 4 Output: 6 10 Am trying to read the input with the help of console class and then convert the String object to primitive type, using Integer.parseInt()  code: import java.io.Console;  public class School {  public static void main(String[] args){     Console c  = System.console();     // to run for the 'T' cases     int t = Integer.parseInt(c.readLine());     for(int a = 0; a&lt;t;a++){         int size = Integer.parseInt(c.readLine());         int [] array = new int[size];         String str = c.readLine();         String [] nums = str.split(&quot; &quot;);         // to convert String array into int arrary         for(int i = 0; i&lt;array.length;i++){             array[i]=Integer.parseInt(nums[i]);         }// end of inner for         int sum = 0;         // for sum         for(int i=0;i&lt;array.length;i++){             sum+=array[i];         }// end of inner for         System.out.println(sum);     }// end of outer for     }// end of main  }// end of school output:  Exception in thread &quot;main&quot; java.lang.NullPointerException at linkedList/gfg.School.main(School.java:9) Is it that c.readLine() isn't able to take the input?
129
What are Null Pointer Exceptions (java.lang.NullPointerException) and what causes them?  What methods/tools can be used to determine the cause so that you stop the exception from causing the program to terminate prematurely?
62286
I need to make some reflective method calls in Java. Those calls will include methods that have arguments that are primitive types (int, double, etc.). The way to specify such types when looking up the method reflectively is int.class, double.class, etc.  The challenge is that I am accepting input from an outside source that will specify the types dynamically. Therefore, I need to come up with these Class references dynamically as well. Imagine a delimited file a list of method names with lists of parameter types:  doSomething int double doSomethingElse java.lang.String boolean   If the input was something like java.lang.String, I know I could use Class.forName("java.lang.String") to that Class instance back. Is there any way to use that method, or another, to get the primitive type Classes back?  Edit: Thanks to all the respondents. It seems clear that there is no built-in way to cleanly do what I want, so I will settle for reusing the ClassUtils class from the Spring framework. It seems to contain a replacement for Class.forName() that will work with my requirements.
106280
Object o = new Long[0] System.out.println( o.getClass().isArray() ) System.out.println( o.getClass().getName() ) Class ofArray = ???   Running the first 3 lines emits;  true [Ljava.lang.Long;   How do I get ??? to be type long?   I could parse the string and do a Class.forname(), but thats grotty.   What's the easy way?
13148
I have an application that reads a CSV file with piles of data rows.  I give the user a summary of the number of rows based on types of data, but I want to make sure that I don't read in too many rows of data and cause OutOfMemoryErrors.  Each row translates into an object.  Is there an easy way to find out the size of that object programmatically?  Is there a reference that defines how large primitive types and object references are for a VM?  Right now, I have code that says read up to 32,000 rows, but I'd also like to have code that says read as many rows as possible until I've used 32MB of memory.  Maybe that is a different question, but I'd still like to know.
7172
What would be the best way to look in a string[] to see if it contains a element. This was my first shot at it. But perhaps there is something that I am overlooking. The array size will be no larger than 200 elements.  bool isStringInArray(string[] strArray, string key) {     for (int i = 0; i &lt;= strArray.Length - 1; i++)         if (strArray[i] == key)             return true;     return false; }
117335
I'm trying to check if an input is an integer and I've gone over it a hundred times but don't see the error in this. Alas it does not work, it triggers the if statement for all inputs (numbers/letters)  read scale if ! [[ "$scale" =~ "^[0-9]+$" ]]         then             echo "Sorry integers only" fi   I've played around with the quotes but either missed it or it did nothing. What do I do wrong? Is there an easier way to test if an input is just an INTEGER?
14528
If you have an array of Java objects which have a primitive type (for example Byte, Integer, Char, etc).  Is there a neat way I can convert it into an array of the primitive type?  In particular can this be done without having to create a new array and loop through the contents.  So for example, given  Integer[] array   what is the neatest way to convert this into  int[] intArray   Unfortunately, this is something we have to do quite frequently when interfacing between Hibernate and some third party libraries over which we have no control.  It seems this would be a quite common operation so I would be surprised if there's no shortcut.  Thanks for your help!
38112
Here's the scenario...    if (entry.Properties["something"].Value != null)   attribs.something = entry.Properties["something"].Value.ToString();   While effective and working correctly, this looks ugly to me.  If I don't check for a null before performing the ToString() then it throws an exception if the property was null.  Is there a better way to handle this scenario?  Much appreciated!
48731
I need to determine the current year in Java as an integer. I could just use java.util.Date(), but it is deprecated.
25568
How is parseInt() different from valueOf() ?   They appear to do exactly the same thing to me (also goes for parseFloat(), parseDouble(), parseLong() etc, how are they different from Long.valueOf(string) ?  Also, which one of these is preferable and used more often by convention?
74579
I am writing this java program to find all the prime numbers up to num using the Sieve of Eratosthenes, but when I try to compile, it says I can't use a long var as an array index, and it expects an int var in its place. But I'll be working with large numbers, so I can't use int. What can I do?  import java.util.*; import java.lang.*;  public class t3{     public static void main(String[] args){         long num = 100;          //declaring list and filling it with numbers         ArrayList&lt;Long&gt; numlist = new ArrayList&lt;Long&gt;();         for(long x=2 ; x&lt;num ; x++){             numlist.add(new Long(x));         }          //sieve or eratosthenes         for(long x=0 ; x&lt;Math.sqrt(num) ; x++){             for(long y=x+1 ; y&lt;numlist.size() ; y++){                 if(numlist[y]%numlist[x] == 0){                     numlist.remove(y);                 }             }         }          //print list         for(Object item : numlist){             System.out.println((Long)item);         }     } }
80715
In javascript, what is the best way to parse an INT from a string which starts with a letter (such as "test[123]")?  I need something that will work for the example below.  My JS:  $(document).ready(function() {      $(':input').change( function() {         id = parseInt($(this).attr("id"));  //fails for the data below         alert(id);     }); }   My generated HTML:  &lt;select id="attribute[123]"&gt; &lt;!-- various options --&gt; &lt;/select&gt; &lt;select id="attribute[456]"&gt; &lt;!-- various options --&gt; &lt;/select&gt;   Thank you!
5691
Is there a better way of getting this result? This function fails if num has more digits than digits, and I feel like it should be in the library somewhere (like Integer.toString(x,"%3d") or something)  static String intToString(int num, int digits) {     StringBuffer s = new StringBuffer(digits);     int zeroes = digits - (int) (Math.log(num) / Math.log(10)) - 1;      for (int i = 0; i &lt; zeroes; i++) {         s.append(0);     }     return s.append(num).toString(); }
11190
When building console applications that take parameters, you can use the arguments passed to Main(string[] args).   In the past I've simply indexed/looped that array and done a few regular expressions to extract the values. However, when the commands get more complicated, the parsing can get pretty ugly.    So I'm interested in:   Libraries that you use Patterns that you use   Assume the commands always adhere to common standards such as .
77422
I have a call to a method in a class as follows:  String key1 = "test1"; String key2 = "test2";  long[] datalr = new long[] {}; datalr = TestClass.callerSub(key1,key2);   The test class is as follows:  public class TestClass {  Public TestClass { }  public long[] callerSub(string key1, String key2){  long[] datalr = new long[] {0,0};  datalr[0] = 2; datalr[1] = 3;  return datalr;  } }   I'm getting the following error in the first class:  Error: Compile Error: Method does not exist or incorrect signature: TestClass.callerSub(String, String) at line 54 column 12       which is the following line:  datalr = TestClass.callerSub(key1,key2);   The datalr data in the callerSub method is just an example above,  Any idea why?  Thanks
30943
I think there must be something subtle going on here that I don't know about. Consider the following:  public class Foo&lt;T&gt; {   private T[] a = (T[]) new Object[5];    public Foo() {     // Add some elements to a   }    public T[] getA() {     return a;   } }   Suppose that your main method contains the following:  Foo&lt;Double&gt; f = new Foo&lt;Double&gt;(); Double[] d = f.getA();   You will get a CastClassException with the message java.lang.Object cannot be cast to java.lang.Double.  Can anyone tell me why? My understanding of ClassCastException is that it is thrown when you try to cast an object to a type that cannot be casted. That is, to a subclass of which it is not an instance (to quote the documentation). e.g.:  Object o = new Double(3.); Double d = (Double) o; // Working cast String s = (String) o; // ClassCastException   And it seems I can do this. If a was just a T instead of an array T[], we can get a and cast it without a problem. Why do arrays break this?  Thanks.
103793
I have a generic class that should allow any type, primitive or otherwise. The only problem with this is using default(T). When you call default on a value type or a string, it initializes it to a reasonable value (such as empty string). When you call default(T) on an object, it returns null. For various reasons we need to ensure that if it is not a primitive type, then we will have a default instance of the type, not null. Here is attempt 1:  T createDefault() {     if(typeof(T).IsValueType)     {         return default(T);     }     else     {         return Activator.CreateInstance&lt;T&gt;();     } }   Problem - string is not a value type, but it does not have a parameterless constructor. So, the current solution is:  T createDefault() {     if(typeof(T).IsValueType || typeof(T).FullName == "System.String")     {         return default(T);     }     else     {         return Activator.CreateInstance&lt;T&gt;();     } }   But this feels like a kludge. Is there a nicer way to handle the string case?