Tag Archives: Java exception

[Solved] asList().add() error: UnsupportedOperationException

ArrayList.add() and aslist().add() problem

using aslist().add() report an error: UnsupportedOperationException


Cause and analysis

asList() is using the internal class ArrayList of Arrays, this internal class does not override the add() method, so the add method used here is the parent class abstract class of the ArrayList class, the add() method here by default is to throw an exception: UnsupportedOperationException

Internal class of arrays

  private static class ArrayList<E> extends AbstractList<E>
        implements RandomAccess, java.io.Serializable
    {
        private static final long serialVersionUID = -2764017481108945198L;
        private final E[] a;

        ArrayList(E[] array) {
            a = Objects.requireNonNull(array);
        }

        @Override
        public int size() {
            return a.length;
        }

        @Override
        public Object[] toArray() {
            return a.clone();
        }

        @Override
        @SuppressWarnings("unchecked")
        public <T> T[] toArray(T[] a) {
            int size = size();
            if (a.length < size)
                return Arrays.copyOf(this.a, size,
                                     (Class<?extends T[]>) a.getClass());
            System.arraycopy(this.a, 0, a, 0, size);
            if (a.length > size)
                a[size] = null;
            return a;
        }

        @Override
        public E get(int index) {
            return a[index];
        }

        @Override
        public E set(int index, E element) {
            E oldValue = a[index];
            a[index] = element;
            return oldValue;
        }

        @Override
        public int indexOf(Object o) {
            E[] a = this.a;
            if (o == null) {
                for (int i = 0; i < a.length; i++)
                    if (a[i] == null)
                        return i;
            } else {
                for (int i = 0; i < a.length; i++)
                    if (o.equals(a[i]))
                        return i;
            }
            return -1;
        }

        @Override
        public boolean contains(Object o) {
            return indexOf(o) != -1;
        }

        @Override
        public Spliterator<E> spliterator() {
            return Spliterators.spliterator(a, Spliterator.ORDERED);
        }

        @Override
        public void forEach(Consumer<?super E> action) {
            Objects.requireNonNull(action);
            for (E e : a) {
                action.accept(e);
            }
        }

        @Override
        public void replaceAll(UnaryOperator<E> operator) {
            Objects.requireNonNull(operator);
            E[] a = this.a;
            for (int i = 0; i < a.length; i++) {
                a[i] = operator.apply(a[i]);
            }
        }

        @Override
        public void sort(Comparator<?super E> c) {
            Arrays.sort(a, c);
        }
    }

Add method of ArrayList() class

 public boolean add(E e) {
        ensureCapacityInternal(size + 1);  // Increments modCount!!
        elementData[size++] = e;
        return true;
    }

Add method of AbstractList class

Aslist() uses this method

public void add(int index, E element) {
        throw new UnsupportedOperationException();//This is where the exception is thrown
    }

Error creating bean with name ‘dataSourceScriptDatabaseInitializer‘ defined in class path resource [

Error creating bean with name ‘dataSourceScriptDatabaseInitializer’ defined in class path resource [org/springframework/boot/autoconfigure/sql/init/DataSourceInitializationConfiguration.class]: Unsatisfied dependency expressed through method ‘dataSourceScriptDatabaseInitializer’ parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘dataSource’ defined in class path resource

Error creating bean with name 'dataSourceScriptDatabaseInitializer' defined in class path resource [org/springframework/boot/autoconfigure/sql/init/DataSourceInitializationConfiguration.class]: Unsatisfied dependency expressed through method 'dataSourceScriptDatabaseInitializer' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource 

The reason for my error is that I made an error when using @springboottest automatic injection when I first learned spring data JPA

@SpringBootTest

//springbootTest The server is not turned on by default,

public class MainTest {
    @Autowired
UserRepository userRepository;

@Test
    void t1(){
    System.out.println(userRepository);
}
}

I searched the Internet for several hours and debugged it. I found that the datasource was always empty, because it was tested and run in the test method at that time, and the errors given were always the same as above. However, I clearly configured the database in application.yml, and later found it during overall debugging, Failed to load driver class com.mysql.cj.jdbc.driver in either of hikariconfig class loader or thread context classloader

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.25</version>
        </dependency>

Of course, it may also be due to other errors. If you also encounter this error, you can see whether you have added MySQL dependencies to your dependencies

Error: unreachable statement [How to Solve]

Error: unreachable statement

The cause of this error is just like its name. The compiler will never execute it
the following situations will occur: unreachable statement:
(1) write a statement after the return statement
(2) write a statement after the throw statement
(3) define statements after the break and continue statements
(4) “\ u10100″// legal, equivalent to ‘\ u1010’ and string “0”
(5) the shift operator can be used for long int char short byte
(6) the access control character of a class can be public or nothing
(7) goto is a reserved word but not a keyword. Then is nothing
(8) when a superclass type is cast and assigned to an object of a subclass, there is no exception in compilation, but an exception will occur at run time
for example:

while(i<length&&len>0){
            if(strs[i].contains(com.substring(0,len))){
                continue;
                i++;
            }else{
                len--;
            }
        }

You can see that the position of I++ in this code is after its continue. After the compiler executes here, it will directly carry out the next cycle, and I++ will never execute.

JAVA ERROR: JDWP Unable to get JNI 1.2 environment, jvm->GetEnv() return code = -2 JDWP exit erro

When running the program is suddenly the following exception occurs

ERROR: JDWP Unable to get JNI 1.2 environment, jvm->GetEnv() return code = -2
JDWP exit error AGENT_ERROR_NO_JNI_ENV(183):  [util.c:840]

The reason:

1.JDK1.6.1
2. There is an error in the last debugging code, which causes the process not to terminate and take up the Console output, and such error occurs when starting debugging later.

Solutions:

At the end of the program, the main() function adds: System.exit(0);
System.exit(0); will cause the program to be terminated immediately, and if there are threads in the program that are still executing tasks, subsequent tasks will not be able to continue.