NullpointerException while reading a json with null value for array [closed] - java

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
Is there a way to read this json without causing nullpointer exception through jackson.
{
"years": [
null
]
}
years is an array of String
Also is that a valid format of json when there are no years?

If you look closely at the tutorials for Jackson, you will see that the JSON this library produces itself for empty arrays uses this notation: { "empty" : [ ]}
Therefore you may try replacing any singular null value in an array with just an empty array before sending your JSON to Jackson, it should accept without throwing any exceptions.
Canonically, a ' null' member of an array is actually valid JSON syntax. See also http://en.wikipedia.org/wiki/JSON. Members of an array can be of any type, thus they can be:
Strings
Numbers
Booleans
Arrays
Objects
null
For your usage scenario however, I would recommend using the empty array instead, because it is simply far easier to program with. For example, consider a usage case where you call some function f() on each of your 'years' which wants an integer input. Code like
foreach(x in array){f(x);} will fail because you will call f() with a 'null' type instead of an integer, causing errors. If you instead used the empty array, the correct behaviour will happen without having to treat the case where there are no years diffrently by explicitly looking for the null. Just makes your life easier that way.

Related

Java how to represent empty Integer in JSON [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I have to represent in JSON null Integer in this way score: . Show name of the attribute and empty space if Integer is null. Now if Integer is null, then JSON doesn't show attribute. Is it possible to do it with Integer?
It is not possible to leave a property value empty in JSON.
The possible value type for the fields are:
Set your variable to something distinctable like -1 or something.
JSON represents data as name/value pairs. There must be a value present if the name is present. See JSON.org for the complete syntax.
A null can be represented by specifying null ...
{name: "Steve", score : null}
... or by omitting the name 'score' from the JSON ...
{name : "Steve"}

Is a multiline toString() output discouraged? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
The javase docs state:
toString()
Returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method.
When handling an object with multiple parameters is it good practice to format the output to display nicely formatted in the console? The default eclipse generated implementation concatenates the variables in a single line which gets troublesome to read, needing to scroll back and forth multiple times and does not group the parameters logically.
I would like to format the output inserting line breaks, but have never actually seen someone doing this before. Will I eventually run into issues with loggers or anything else or is it perfectly fine to format the output of toString()the way I want?
Is it better to implement a additional method toStringPretty()?
Usually it is not discouraged to avoid producing multi-line string from toString().
as java doc said it should be concise and informative but if you find yourself in a situation where lots of field should be formatted and represented in log file, notice that nobody looks for pretty log. the log should be searchable with regex to facilitate the finding what you are looking for.
What i can suggest you is that instead of formatting multi-line for readability in log file, print your elements in a single line Json format which is both easy to read and search, but if you what to show an output on console to the user it is better off to extract the formatting logic outside of the toString()

Counting the repetition of an element of an array java [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I need to count the frequency of an element in arrays.
I used the method
Collections.frequency(Arrays.asList(arr),element);
but I get zero all the times
any ideas ?!
If you are ArrayList consists of elements of custom type
example person bean, or employee object.
Make sure you have overridden equals() method and hash() methods
if you have not overridden these methods that Collection method wont work.
You need to give details about "arr" & element. However, I did came across this some time back when I tried to use an array of primitives such as int[], converting them to a List using Arrays.asList()
There is nothing like List of "int". An Integer would work however, Integer arr[] = {1,1,1,1,3,3,4,5,5,5,6};

Search a string array for a specific string. [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I was hoping someone could help. Well I know how to use linear search to find a numerical value inside of an array. But now I want to learn how to search a string array for a string.
For example if I have a string array of students names how could I search the array to find a specific name inside that array?
If someone could write me a simple example since I'm new to Java still. Thank you, also is there a better way to search these kinds of things or linear search fine? :)
Use Arrays.asList() to wrap the array in a List. Then search in the List using contains() method:
String[] a= {"is", "are", "then"};
Arrays.asList(a).contains("are");
You can do using arrays also, but have to write some more lines of code. contains() method also does the linear search only.
public static boolean useLoop(String[] arr, String targetValue) {
for(String s: arr){
if(s.equals(targetValue))
return true;
}
return false;
}
The code for Strings can be the same as for numerical values, except you should use the String class's equals(...) method instead of ==.
You should use equals(...) because it's possible for two different String objects to have the same characters. But == would be incorrect because they are different objects.
Examples comparison expressions:
myArray[i].equals(myString)
or
myString.equals(myArray[i])
Note: Be sure the part on the left side isn't null. Otherwise you will get a NullPointerException.

Importance of using JSON objects compared to large strings of data? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have recently started using JSON and was wondering if I am missing any other important reasons as to why I would use a JSON object instead of just returning a large string of data.
Here is what I have found so far:
A JSON object is a lot faster to process, easier to handle and parse. A JSON object is easier for a human to read compared to a big string of data output. JSON objects can be mapped easier and works well with object oriented systems.
JSON is just a [large] string of data.
The difference from a one-off/custom "blob'o'test" encoding is that JSON is a well-defined format that supports common ADTs (Arrays, Maps) and is a useful interchange format. Also, one doesn't work with JSON (which is just text) directly; one works with the object-graphs that are serialized to/from JSON - e.g. once you call JSON.parse(jsonText) you're dealing with regular objects.
While XML is another well-defined format, JSON has a better 1-1 mapping with simple object-graphs. This easier mapping eliminates the need for a specific DOM wrapper or other specialized access - who wants to deal with an object model when one can treat an object-graph as first-class data?1
The fact that JSON (which is just text) also looks like normal JavaScript Object Literal Notation (and excluding some odd Unicode issues, is a subset) makes human consumption relatively easy and has greatly helped the adoption.
Refer to the following questions for additional insight on "What?" and "Why?"
What is JSON and why would I use it?
What is the exact use of JSON?
Why use JSON instead a normal html output with AJAX?
Why is it a bad practice to return generated HTML instead of JSON? Or is it?
https://stackoverflow.com/questions/3536893/what-are-the-pros-and-cons-of-xml-and-json
Why is Everyone Choosing JSON Over XML for jQuery?
1 XML is much more than a simple markup format, but comparing XML to JSON in any more detail is outside the scope of the question.

Categories