小编Krz*_*ski的帖子

使用方法引用而不是lambda表达式抛出java.lang.NullPointerException

我注意到使用Java 8方法引用的未处理异常有些奇怪.这是我的代码,使用lambda表达式() -> s.toLowerCase():

public class Test {

    public static void main(String[] args) {
        testNPE(null);
    }

    private static void testNPE(String s) {
        Thread t = new Thread(() -> s.toLowerCase());
//        Thread t = new Thread(s::toLowerCase);
        t.setUncaughtExceptionHandler((t1, e) -> System.out.println("Exception!"));
        t.start();
    }
}
Run Code Online (Sandbox Code Playgroud)

它打印"Exception",所以它工作正常.但是当我Thread t改为使用方法引用时(甚至IntelliJ建议):

Thread t = new Thread(s::toLowerCase);
Run Code Online (Sandbox Code Playgroud)

异常没有被捕获:

Exception in thread "main" java.lang.NullPointerException
    at Test.testNPE(Test.java:9)
    at Test.main(Test.java:4)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Run Code Online (Sandbox Code Playgroud)

有人能解释一下这里发生了什么吗?

java lambda nullpointerexception java-8 method-reference

44
推荐指数
2
解决办法
3597
查看次数

无法加载JDBC驱动程序类[com.mysql.jdbc.Driver]

所以,这是我的项目结构:

在此输入图像描述

在里面cats-application我有CatsDAO类,我试图@Autowire DataSource反对:

import javax.sql.DataSource;

@Repository
public class CatsDAO {

    @Autowired
    private DataSource dataSource;

    public Cat getCat(String name) {
        String sql = "SELECT * FROM cats WHERE name = ?";

        Connection conn = null;
        try {
            ...
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            ...
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我applicationContext.xmlcats-webapp:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <context:component-scan base-package="ninja.majewski"/>
    <context:annotation-config/>
    <mvc:annotation-driven/>

    <bean …
Run Code Online (Sandbox Code Playgroud)

java mysql spring jdbc spring-mvc

8
推荐指数
2
解决办法
4万
查看次数

在 libGDX 中动态更改窗口标题

如何在游戏过程中更改窗口标题?我找不到任何方法。例如,我想在标题栏中显示我得了多少分。

desktop libgdx

7
推荐指数
1
解决办法
2200
查看次数

Mongodb 与 docker-compose - 创建用户

这是我的 docker-compose.yaml:

version: "2.0"
services:
  mongo_container:
    image: mongo:latest
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: example
      MONGO_INITDB_DATABASE: testdb
    ports:
      - "27017:27017"
    volumes:
      - ./mongodata:/data/db
Run Code Online (Sandbox Code Playgroud)

这在我的弹簧配置中:

spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
spring.data.mongodb.username=root
spring.data.mongodb.password=example
spring.data.mongodb.database=testdb
Run Code Online (Sandbox Code Playgroud)

但是每次当我尝试将我的应用程序连接到 Mongo 时,我都会在 Docker 控制台中收到以下错误:

mongo_container_1  | 2020-03-31T07:37:24.803+0000 I  ACCESS   [conn2] SASL SCRAM-SHA-1 authentication failed for root on testdb from client 172.29.0.1:36628 ; UserNotFound: Could not find user "root" for db "testdb"
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?我尝试删除所有容器并docker system prune再次运行它,但它仍然给出相同的错误。

mongodb docker spring-boot docker-compose

6
推荐指数
1
解决办法
3085
查看次数

Spring MockMvc - 从 REST 获取 java.time.Instant

我有一个资源,它返回一个具有java.time.Instant属性的对象。

class X {
    ...
    private Instant startDate;
    ...
}
Run Code Online (Sandbox Code Playgroud)

我正在测试它:

    mockMvc.perform(get("/api/x"))
        .andExpect(status().isOk())
        .andExpect(jsonPath("$.content.[*].startDate").value(hasItem(MY_INSTANT_DATE)));
Run Code Online (Sandbox Code Playgroud)

但我从 JUnit 得到的是:

Expected: a collection containing <2018-06-08T11:46:50.292Z> but: was <1528458378.397000000>
Run Code Online (Sandbox Code Playgroud)

如何将我的Instant日期映射为这种格式?

java junit json spring-mvc mockmvc

5
推荐指数
1
解决办法
1157
查看次数

仅授予对 postgreSQL 中一个模式的访问权限

我有一个用户just_one_schema_user

在我的数据库中,我有两个模式:publicsample

我怎样才能让这个用户只看到sample

这就是我所做的:

GRANT USAGE ON SCHEMA sample TO just_one_schema_user

REVOKE ALL PRIVILEGES ON SCHEMA public FROM just_one_schema_user

但用户仍然可以列出其中的表public并查看它们的结构。

sql database postgresql database-schema pgadmin

5
推荐指数
1
解决办法
6734
查看次数

Apache POI Excel折线图点

我正在创建带有折线图的Excel文件。我已经创建了图表并用数据填充了它,但是无法在图表上创建点。有谁知道,有一种方法可以使用apache poi在图表中生成这些点(三角形,正方形,圆形等)?

在此处输入图片说明

这是我用于生成当前char的代码:

    public static void main(String[] args) throws Exception {
        Workbook wb = new XSSFWorkbook();
        Sheet dataSheet = wb.createSheet("linechart");

        final int NUM_OF_ROWS = 10;
        final int NUM_OF_COLUMNS = 4;

        Row row;
        Cell cell;
        for (int rowIndex = 0; rowIndex < NUM_OF_ROWS; rowIndex++) {
            row = dataSheet.createRow((short) rowIndex);
            for (int colIndex = 0; colIndex < NUM_OF_COLUMNS; colIndex++) {
                cell = row.createCell((short) colIndex);
                cell.setCellValue(rowIndex * ((colIndex + 1) + ((int) (Math.random() * 10))));
            }
        }

        Drawing drawing = dataSheet.createDrawingPatriarch();
        ClientAnchor anchor …
Run Code Online (Sandbox Code Playgroud)

java apache excel charts apache-poi

4
推荐指数
1
解决办法
4933
查看次数

为什么选择了重载基类值的默认参数值,而不是执行的派生方法中指定的默认值

在玩逻辑转换时我注意到了一件奇怪的事情.这是我的代码:

#include <iostream>

using namespace std;
class C
{
public:
    virtual int shift(int n = 2) const { return n << 2; }
};
class D
    : public C
{
public:
    int shift(int n = 1) const { return n << 5; }
};
int main()
{
    const D d;
    const C *c = &d;
    cout << c->shift() << endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

程序返回64,因此从类C中获取值n = 2,从类D中获取函数的主体.

从函数中删除const后它工作正常,但我不明白为什么.有人能帮我吗 ?

c++

3
推荐指数
1
解决办法
69
查看次数

“无法解析方法”与嘲笑

我使用org.springframework.security.core.Authentication其中的一种方法:

Collection<? extends GrantedAuthority> getAuthorities();
Run Code Online (Sandbox Code Playgroud)

我想模拟如下:

when(authentication.getAuthorities()).thenReturn(grantedAuthorities);
Run Code Online (Sandbox Code Playgroud)

与当局收集:

Collection<SimpleGrantedAuthority> grantedAuthorities = Lists.newArrayList(
        new SimpleGrantedAuthority(AuthoritiesConstants.USER));
Run Code Online (Sandbox Code Playgroud)

我正在使用org.springframework.security.core.authority.SimpleGrantedAuthority扩展GrantedAuthority

Intellij给了我下面的编译错误:

Cannot resolve method 'thenReturn(java.util.Collection<org.spring.security.core.authority.SimpleGrantedAuthority>)'
Run Code Online (Sandbox Code Playgroud)

我使用Mockito 2.15.0thenReturn()方法是:

OngoingStubbing<T> thenReturn(T value);
Run Code Online (Sandbox Code Playgroud)

问题是什么?

junit spring spring-security mockito

3
推荐指数
2
解决办法
2871
查看次数

在 Intellij 中为 TODO 定义模板

当我创建 todo 时,它默认只添加日期:

// TODO: 2017-03-02  
Run Code Online (Sandbox Code Playgroud)

我想将此模板更改为其他内容,例如:

// TODO: 2017-03-02  Created by: @MyName
Run Code Online (Sandbox Code Playgroud)

有没有办法做到这一点?

intellij-idea todo

2
推荐指数
1
解决办法
764
查看次数

android:layout_weight无法正常工作

我想在一个TableRow中使用TextView和EditText,我希望EditText的宽度比TextView大两倍,所以我把它放在TextView区域:

android:layout_weight="1"
Run Code Online (Sandbox Code Playgroud)

在EditText区域:

android:layout_weight="2"
Run Code Online (Sandbox Code Playgroud)

但它看起来不正确:

在此输入图像描述

我的整个.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TableRow
        android:id="@+id/imageButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:padding="8dp" >

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:maxHeight="150dp"
            android:maxWidth="150dp"
            android:scaleType="centerInside"
            android:src="@drawable/add_user" />
    </TableRow>

    <TableRow
        android:id="@+id/loginRow"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="8dp" >

        <TextView
            android:id="@+id/loginTextView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/login_text_view" />

        <EditText
            android:id="@+id/loginEditText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:inputType="text" >

            <requestFocus />
        </EditText>
    </TableRow>

    <TableRow
        android:id="@+id/passwordRow"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="8dp" >

        <TextView
            android:id="@+id/passwordTextView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/password_text_view" />

        <EditText
            android:id="@+id/passwordEditText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:inputType="textPassword" />
    </TableRow>

    <TableRow
        android:id="@+id/nameRow"
        android:layout_width="match_parent" …
Run Code Online (Sandbox Code Playgroud)

xml android android-layout

1
推荐指数
1
解决办法
1447
查看次数

模板专业化函数c ++

这是我的代码:

#include <iostream>
using namespace std;

template< typename T >
T silnia( T w ) {
    cout << "not special" << endl;
}
template<>
int silnia<int>( int x ) {
    cout << "special" << endl;
}

int main() {

cout << silnia<double>(5) << endl;
cout << silnia<int>(5) << endl;

return 0;
}
Run Code Online (Sandbox Code Playgroud)

这是输出:

not special
nan
special
4712544
Run Code Online (Sandbox Code Playgroud)

有人可以帮我理解哪两个额外的线来自哪里?

c++ templates cout

0
推荐指数
1
解决办法
46
查看次数

std :: pair在std :: map中使用

我的程序有问题.到达时我收到错误

cout << it->second << endl;
Run Code Online (Sandbox Code Playgroud)

我的节目:

#include <iostream>
#include <map>
#include <algorithm>
using namespace std;

int main() {
    map<pair<int, int>, int> kwadraty;
    long long ile;
    cin >> ile;
    int temp1, temp2;
    for(int i = 0; i < ile; i++)
    {
        cin >> temp1 >> temp2;
        kwadraty[pair<int, int>(temp1, temp2)]++;
    }

    for(map<pair<int, int>, int>::iterator it; it != kwadraty.end(); it++)
    {
        cout << it->second << endl;
    }

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

任何人都可以看到问题出在哪里?

c++ c++11

-1
推荐指数
1
解决办法
67
查看次数