How-to articles, tricks, and solutions about JAVA

GSON throwing "Expected BEGIN_OBJECT but was BEGIN_ARRAY"?

If GSON is throwing a JsonSyntaxException with the message "Expected BEGIN_OBJECT but was BEGIN_ARRAY", it means that you are trying to parse a JSON array as if it were a JSON object.

HashMap - getting First Key value

To get the first key-value pair from a HashMap in Java, you can use the entrySet() method to get a set of the map's entries and then use the iterator() method to get an iterator for the set.

HashMap with multiple values under the same key

To store multiple values under the same key in a HashMap in Java, you can use a List or an array as the value for the key.

Hibernate show real SQL

To show the real SQL generated by Hibernate, you can enable the show_sql property in the Hibernate configuration file (hibernate.cfg.xml).

Hibernate throws MultipleBagFetchException - cannot simultaneously fetch multiple bags

If you are using Hibernate and you see the error "MultipleBagFetchException: cannot simultaneously fetch multiple bags", it means that you are trying to fetch multiple collections of an entity using a single Hibernate query.

How can I ask the Selenium-WebDriver to wait for few seconds in Java?

To make the Selenium WebDriver wait for a specified number of seconds in Java, you can use the Thread.sleep method. Here's an example of how you can do this:

How can I check if a single character appears in a string?

To check if a single character appears in a string in Java, you can use the indexOf method of the String class.

How can I check if a value is of type Integer?

To check if a value is of type Integer in Java, you can use the instanceof operator.

How can I check if an element exists with Selenium WebDriver?

To check if an element exists with Selenium WebDriver, you can use the findElements() method of the WebDriver interface and check the size of the returned list.

How can I check whether an array is null / empty?

To check if an array is null in Java, you can use the == operator and compare the array to null. For example:

How can I clear or empty a StringBuilder?

To clear or empty a StringBuilder in Java, you can use the setLength method and set the length to 0.

How can I concatenate two arrays in Java?

To concatenate two arrays in Java, you can use the System.arraycopy method. Here's an example of how you can do this:

How can I convert a long to int in Java?

To convert a long value to an int value in Java, you can use the intValue method of the java.lang.Long class. This method returns the value of the specified long as an int.

How can I convert a stack trace to a string?

To convert a stack trace to a string in Java, you can use the printStackTrace() method of the Throwable class, which prints the stack trace to a PrintWriter.

How can I convert List<Integer> to int[] in Java?

You can use the toArray() method of the List interface to convert a List<Integer> to an int[] in Java.

How can I convert my Java program to an .exe file?

There are several ways to convert a Java program to an executable file (.exe):

How can I create a memory leak in Java?

It is generally not a good idea to intentionally create a memory leak in Java.

How can I create an executable/runnable JAR with dependencies using Maven?

To create an executable JAR with dependencies using Maven, you can use the maven-assembly-plugin. This plugin allows you to package your project and its dependencies into a single JAR file.

How can I download and save a file from the Internet using Java?

To download and save a file from the Internet using Java, you can use the URL and URLConnection classes from the java.net package.

How can I edit a .jar file?

A .jar file is a Java Archive file that contains compiled Java code and resources (such as images, sounds, etc.) that can be used by Java applications.

How can I fix 'android.os.NetworkOnMainThreadException'?

The android.os.NetworkOnMainThreadException is a runtime exception that is thrown when an application attempts to perform a networking operation on the main thread.

How can I generate an MD5 hash in Java?

To generate an MD5 hash in Java, you can use the MessageDigest class from the java.security package.

How can I generate random number in specific range in Android?

To generate a random number in a specific range in Android, you can use the nextInt method of the java.util.Random class.

How can I get the current date and time in UTC or GMT in Java?

To get the current date and time in UTC or GMT in Java, you can use the Instant class from the java.time package. The Instant class represents a single point in time in the ISO-8601 calendar system, with a resolution of nanoseconds.

How can I get the current stack trace in Java?

To get the current stack trace in Java, you can use the getStackTrace method of the Thread class or the getStackTrace method of the Throwable class.