Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8402449
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:04:07+00:00 2026-06-09T22:04:07+00:00

I get an java null exception for the bitmap b in the following code:

  • 0

I get an java null exception for the bitmap b in the following code: what could i do?
Why doesn’t the getDrawingCache function work in creating the bitmap;
The onClickListener for the button that starts the action:

KaufenGListener = new OnClickListener() {

            @Override
            public void onClick(View v) {

                Coupon coupon = null;

                System.out.println("-- iv: " + couponView);

                if (clickedcoup == -1) {
                    Toast.makeText(getActivity(), "Please select a coupon", Toast.LENGTH_SHORT).show();
                } else {
                    for (int i = 0; i < coupons.size(); i++) {
                        if (clickedcoup == i) {
                            coupon = coupons.get(i);
                        }
                    }
                    String username;
                    Bitmap coupo = BitmapFactory.decodeResource(getResources(), R.drawable.coupon1);
                    Bitmap barcode = Util.generateEAN("9310779300005", getActivity(), coupo.getWidth() / 3);
                    // Drawable couponDraw = new
                    // BitmapDrawable(getResources(),b);
                    System.out.println("------------b : " + barcode);
                    if (user.address.Nachname != null) {
                        username = user.address.Nachname + " " + user.address.Vorname; // bla
                    } else {
                        username = "";
                    }
                    viewFlipperVino.setDisplayedChild(8);
                    // LayoutParams params = new LayoutParams();
                    // params.width=LayoutParams.MATCH_PARENT;
                    // params.height=(int) (params.width/1.5);

                    //couponView.buildDrawingCache();
                    couponView.setUp(coupo, barcode, coupon.Code, gutscheinName, username, coupon.Wert);
                    clickedcoup = -1;
                    String[] recipients = new String[] { "rosu_alin@ymail.com" };
                    String emailSubject = "Gutschein Schenken fur "+ gutscheinName;
                    String emailText = "Glückwunsch, Sie haben einen Gutschein für Wein & Co!!";
                    Intent intent = new Intent(Intent.ACTION_SEND);
                    intent.setType("plain/text");
                    intent.putExtra(Intent.EXTRA_EMAIL, recipients);
                    intent.putExtra(Intent.EXTRA_SUBJECT, emailSubject);
                    intent.putExtra(Intent.EXTRA_TEXT, emailText);
                    File root = Environment.getExternalStorageDirectory();
                    File file = new File(root, "gutschein.png");
                    try {
                        file.createNewFile();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }                       
                    couponView.setDrawingCacheEnabled(true);
                    couponView.buildDrawingCache();
                    Bitmap b = couponView.getDrawingCache();
                    System.out.println("bitmapget drawin cache:   "+couponView.getDrawingCache());
                    Saver.saveImg(file, b, getActivity());
                    try {
                        System.out.println("bitmap"+b);
                           FileOutputStream out = new FileOutputStream(file);
                           b.compress(Bitmap.CompressFormat.PNG, 100, out);
                           System.out.println("bitmap.compress"+b.compress(Bitmap.CompressFormat.PNG, 100, out));
                    } catch (Exception e) {
                           e.printStackTrace();
                    }

                    if (!file.exists() || !file.canRead()) {
                        Toast.makeText(getActivity(), "Befestigung Fehler", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    Uri uri = Uri.parse("file://" + file);
                    intent.putExtra(Intent.EXTRA_STREAM, uri);
                    startActivity(Intent.createChooser(intent, "Send email..."));
                }
            }

        };

This is the CouponView class that extends View:
public class CouponView extends View {

private Bitmap coupon, barcode;
Paint paint = new Paint();
Paint paint2 = new Paint();
Paint paint3 = new Paint();
private String name;
private String code;
private String username;
private String wert;
int w;
int h;

public CouponView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init();
}

public CouponView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
}

public CouponView(Context context) {
    super(context);
    init();
}

private void init() {

    paint.setColor(0xFF000000);
    paint.setTextSize(7);
    paint2.setColor(0xFF000000);
    paint2.setTextSize(10);
    paint3.setColor(0xFF000000);
    paint3.setTextSize(25);

}

public void setUp(Bitmap coupon, Bitmap barcode, String code, String name, String username,String wert) {
    this.coupon = coupon;
    this.barcode = barcode;
    this.name = name;
    this.username = username;
    this.code = code;
    this.wert = wert;
     w = coupon.getWidth();
     h = coupon.getHeight();
    System.out.println("height"+h);
    System.out.println("width"+w);

    invalidate();
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    System.out.println("-------------CouponView.onDraw()");
    canvas.drawBitmap(coupon, 0, 0, null);
    canvas.drawBitmap(barcode, w/1.61f, h/1.20f, null);
    canvas.drawText("9310779300005", w/1.429f, h/1.018f, paint);
    canvas.drawText("9310779300005", w/18.12f , h/1.073f, paint2);
    canvas.drawText(code, w/1.8875f, h/1.073f, paint2);
    canvas.drawText(username, w/11.32f, h/10.3f, paint2);
    canvas.drawText(name, w/11.32f, h/5.936f, paint2);
    canvas.drawText(wert, w/1.294f, h/1.641f, paint3);
}
}

After my logic, I set the drawingcache for the couponView to (true) and then i start building the drawingcache. And then i initiate to the bitmap the getDrawingCache(). That alone should work on saving my file.What am i missing?
Also the Saves.saveIMG function does the same thing as the try catch after it (i only took it from a colleague because it was more organized. and i though it uses the imageView to save, not the bitmap.

public class Saver {
public static void saveImg(File pic, Bitmap picture,Context context) {
    folderExists(pic);
    try {
        FileOutputStream out = new FileOutputStream(pic);
        picture.compress(Bitmap.CompressFormat.JPEG, 100, out);
        Log.i(Saver.class.toString(), " picture writed at: " + pic.getAbsolutePath());
        Toast.makeText(context, "pic saved", Toast.LENGTH_SHORT).show();
    } catch (Exception e) {
        pic.delete();
        Log.i(Saver.class.toString(), " picture writing error: " + e.getMessage());
        Toast.makeText(context, "pic not saved", Toast.LENGTH_SHORT).show();
    }
}

private static void folderExists(File dir) {
    System.out.println("-=-=- : " + dir.getAbsolutePath());
    if (!dir.getParentFile().exists()) {
        dir.mkdirs();
    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T22:04:09+00:00Added an answer on June 9, 2026 at 10:04 pm

    I first created an empty bitmap, and used it as a canvas to draw into it.
    Code:

    couponView.setDrawingCacheEnabled(true);
      couponView.buildDrawingCache();
      couponView.setUp(coupo, barcode, coupon.Code, gutscheinName, username, coupon.Wert);
      Bitmap b = Bitmap.createBitmap(couponView.w, couponView.h, 
                                Config.ARGB_8888);
      Canvas canvas = new Canvas(b);
      couponView.draw(canvas);
      Saver.saveImg(file, b, getActivity());
      Uri uri = Uri.parse("file://" + file);
      intent.putExtra(Intent.EXTRA_STREAM, uri);
      startActivity(Intent.createChooser(intent, "Send email..."));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I sometimes get a null pointer exception with my code. Oddly enough, this does
In Java, is it possible to attempt a cast and get back null if
When i am generating a report, I get java.sql.SQLException: ORA-08103: object no longer exists.Code
When I put the below function into leon repl , I get java.lang.IllegalArgumentException: Wrong
I get a java.lang.outOfMemoryError exception, while writing a big file to the servletOutputStream. Every
I've never been able to get the Java 6 splash screen to work. I've
I'm trying to implement an interface, but I get the 'Null-pointer' exception, but I
I get a null pointer exception when a call a method on a custom
This code throws a null pointer exception at line 20, where the compareTo() is
Possible Duplicate: Why do I get a null pointer exception from TabWidget? I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.