Java Snippets
786 snippets in Java.
- :: (double colon) operator in Java 8
In Java 8, the :: (double colon) operator is used to refer to a method or a constructor.
- 'Field required a bean of type that could not be found.' error spring restful API using mongodb
The 'Field required a bean of type that could not be found' error in a Spring application typically indicates that the application is trying to inject a dependency into a field, but it cannot find a bean of the required type in the application context.
- 'Java' is not recognized as an internal or external command
If you are getting the error 'Java' is not recognized as an internal or external command, it means that the Java executable is not in your system's PATH. This means that when you try to run the java command, the system does not know where to find it.
- "Content is not allowed in prolog" when parsing perfectly valid XML on GAE
The "Content is not allowed in prolog" error typically occurs when you try to parse an XML document that contains characters before the XML prolog (the <?xml ...?> declaration).
- "implements Runnable" vs "extends Thread" in Java
In Java, you can create a thread in two ways: by implementing the Runnable interface or by extending the Thread class.
- "No X11 DISPLAY variable" - what does it mean?
"No X11 DISPLAY variable" is an error message that you might see when trying to run a Java program that uses the X Window System to display a graphical user interface (GUI).
- @Autowired - No qualifying bean of type found for dependency
No qualifying bean of type found for dependency is an error message that can occur when you are using the @Autowired annotation in Spring to inject a bean dependency.
- @RequestParam vs @PathVariable
In Spring MVC, the @RequestParam annotation is used to bind a request parameter to a method parameter.
- && (AND) and || (OR) in IF statements
In Java, the && and || operators are used in if statements to combine multiple conditions.
- A for-loop to iterate over an enum in Java
To iterate over an enum in Java using a for loop, you can use the values() method of the enum type to get an array of all the enum values, and then use a standard for loop to iterate over the array.
- A Java collection of value pairs? (tuples?)
In Java, you can use the AbstractMap.SimpleEntry class from the java.util package to represent a value pair (also known as a tuple).
- A KeyValuePair in Java
In Java, a KeyValuePair is a data structure that represents a pair of keys and values, similar to a Map.
- A quick and easy way to join array elements with a separator (the opposite of split) in Java
To join array elements with a separator in Java, you can use the join method from the String class.
- Accept server's self-signed ssl certificate in Java client
To accept a server's self-signed SSL certificate in a Java client, you can create a custom javax.net.ssl.X509TrustManager and use it to override the default trust manager.
- Access restriction on class due to restriction on required library rt.jar?
If you are getting an "access restriction" error on a class in your Java code, it means that you are trying to access a class or member (field or method) that has restricted access.
- accessing a variable from another class
To access a variable from another class in Java, you can use the following steps:
- add an element to int [] array in java
To add an element to an array in Java, you can use one of the following methods:
- Add context path to Spring Boot application
To add a context path to a Spring Boot application, you can use the server.context-path property in the application's application.properties file.
- Add leading zeroes to number in Java?
To add leading zeroes to a number in Java, you can use the format method of the String class, along with a format string that specifies the desired length and padding character.
- add string to String array
To add a string to a string array in Java, you can use the Arrays.copyOf() method to create a new array that is one element larger than the original array, and then use a loop to copy the elements of the original array into the new array. Here's an exampl
- Adding header for HttpURLConnection
To add a header to an HTTP request using HttpURLConnection, you can use the setRequestProperty method.
- Android changing Floating Action Button color
To change the color of a Floating Action Button (FAB) in Android, you can use the setBackgroundTintList() method and pass it a color state list.
- android on Text Change Listener
In Android, you can use a TextWatcher to listen for changes to the text in a TextView or EditText view.
- Android SDK installation doesn't find JDK
If the Android SDK installation is unable to find the JDK (Java Development Kit), it could be because the JDK is not installed or is not installed in the default location.
- Android Split string
To split a string in Android, you can use the split() method of the String class.
- Any shortcut to initialize all array elements to zero?
To initialize all elements of an array to zero in Java, you can use the Arrays.fill method from the java.util package.
- Append a single character to a string or char array in java?
To append a single character to a string or char array in Java, you can use the + operator or the concat method for strings, or you can use the Arrays.copyOf method for char arrays.
- ArrayList of int array in java
To create an ArrayList of int arrays in Java, you can use the following syntax:
- Avoiding NullPointerException in Java
A NullPointerException is a runtime exception that is thrown when an application attempts to use an object reference that has a null value. To avoid this exception, you need to make sure that the object reference is not null before you use it.
- Base64 Java encode and decode a string
To encode and decode a string in Base64 in Java, you can use the java.util.Base64 class.