我注意到使用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)
有人能解释一下这里发生了什么吗?
所以,这是我的项目结构:
在里面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.xml的cats-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) 这是我的 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再次运行它,但它仍然给出相同的错误。
我有一个资源,它返回一个具有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日期映射为这种格式?
我有一个用户just_one_schema_user。
在我的数据库中,我有两个模式:public和sample
我怎样才能让这个用户只看到sample?
这就是我所做的:
GRANT USAGE ON SCHEMA sample TO just_one_schema_user
REVOKE ALL PRIVILEGES ON SCHEMA public FROM just_one_schema_user
但用户仍然可以列出其中的表public并查看它们的结构。
我正在创建带有折线图的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) 在玩逻辑转换时我注意到了一件奇怪的事情.这是我的代码:
#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后它工作正常,但我不明白为什么.有人能帮我吗 ?
我使用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.0,thenReturn()方法是:
OngoingStubbing<T> thenReturn(T value);
Run Code Online (Sandbox Code Playgroud)
问题是什么?
当我创建 todo 时,它默认只添加日期:
// TODO: 2017-03-02
Run Code Online (Sandbox Code Playgroud)
我想将此模板更改为其他内容,例如:
// TODO: 2017-03-02 Created by: @MyName
Run Code Online (Sandbox Code Playgroud)
有没有办法做到这一点?
我想在一个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) 这是我的代码:
#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)
有人可以帮我理解哪两个额外的线来自哪里?
我的程序有问题.到达时我收到错误
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)
任何人都可以看到问题出在哪里?
java ×4
c++ ×3
junit ×2
spring ×2
spring-mvc ×2
android ×1
apache ×1
apache-poi ×1
c++11 ×1
charts ×1
cout ×1
database ×1
desktop ×1
docker ×1
excel ×1
java-8 ×1
jdbc ×1
json ×1
lambda ×1
libgdx ×1
mockito ×1
mockmvc ×1
mongodb ×1
mysql ×1
pgadmin ×1
postgresql ×1
spring-boot ×1
sql ×1
templates ×1
todo ×1
xml ×1